Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
7/29/2008 5:37:49 PM EDT
Hey all. What I'm trying to do is write a script in Linux that will simply check to see if a specific process is running, and if it is not, do such and such. Something like using 'ps ex | grep [process name]' and pulling the process name into a variable, then checking the variable maybe? I'm not sure, but it's proving a lot harder than I thought it should be. Thanks.
7/29/2008 5:50:45 PM EDT
[#1]
check any of the scripts in init.d for examples of how to do this.
7/29/2008 6:02:16 PM EDT
[#2]
Replace the echo with whatever command you want it to do and replace the sshd with whatever process you are looking for.


height=8
pgrep sshd || echo 'not running'
7/29/2008 7:04:37 PM EDT
[#3]

Quoted:
Replace the echo with whatever command you want it to do and replace the sshd with whatever process you are looking for.



pgrep sshd || echo 'not running'


I think this may be the ticket. Thanks!
7/30/2008 7:44:21 AM EDT
[#4]
You can also use pidof
7/30/2008 11:07:09 AM EDT
[#5]
Ok, this is what I came up with, and it seems to work:

#!/bin/sh
# Simple reboot script by Slink.
# If used to start the Mud, will automatically reboot if
# it goes down.

if [ -r SHUTDOWN.TXT ];
then
rm SHUTDOWN.TXT
./cygma
fi

while [ 1 ]
do
if [ -r SHUTDOWN.TXT ];
then
echo -e '\r'
date
echo 'Mud has shut down. Exitting reboot script.'
break
fi
pgrep cygma || sleep 5;echo -e '\r';date;echo 'Starting reboot script.';echo -e '\r';./cygma
done

***
I've also written a short script that works with my makefile that allows a specification to back up all my project folders. This seems to work, but I would now like to add a "restore" specification that will delete the current project files/directories, rename the backup directory to the current directory, and then make the new backup a copy of the new current directory (Thus deleting anything and everything I have changed since the last backup, and rolling it back.) The problem is, this script is to be run from inside one of the directories I am trying to delete, and I can't delete a directory that is being used. Any ideas? I'm new to this, so please be specific. Thanks!

This is my current makefile:

make:
@echo Make std to compile.
@echo Make clean to clean up.
@echo Make back to back up all files.

std:
make -f makefile.std

standard:
std

all:
std

clean:
rm -f *.o cygma

back:
rm -f *.o cygma
rm -f -r /'Copy of Cygma'
cp -r /Cygma /'Copy of Cygma'

backup:
back
7/30/2008 11:11:07 AM EDT
[#6]
Specify the path to SHUTDOWN.txt.  That way it's valid regardless of the CWD at the time you execute the script.  Even better, stick it into a variable and reference the variable rather than typing out the path name every where in the script.

Also for the sake of completeness it doesn't hurt to set your path in advance, and any other environment you rely upon.

And use while : instead of while [ 1 ] (it's just more efficient, dammit.)  Or use 'while true' which is equivalent to while : in this shell.

ETA: What you are doing looks like it should be done via init scripts.  Check those out.  There are tons of things that occur when a system is shutting down.


Regarding your makefile -- consider not deleting directories, only files -- or consider running it from outside the directory you want deleted.


ETA2: Yeah, you definitely would be best off putting this into an init script for both startup and shutdown of your process.  The script would be much simplified as a result.

ETA3: Actually in place of your makefile you might want to look into revision control as it sounds like it'd suit your purposes.
7/30/2008 11:53:56 AM EDT
[#7]
Thanks for the post. I will look into all that. As I said, I'm just starting out. I began attempting scripts for the first time yesterday, so I really appreciate all the advice!
7/30/2008 2:22:52 PM EDT
[#8]
dupe, sorry
7/30/2008 2:22:52 PM EDT
[#9]
Or you could run the script out of inittab and automagically get the process running at boot.

Pay special attention to the -respawn parm.

Not all linux distributions ship with inittab, apparently, in which case you need to explore upstart as an alternative.

Hey, if you're going to mess with Linux, you'll eventually want to to graduate from kindergarten... <grin>
7/30/2008 2:43:57 PM EDT
[#10]

Quoted:
Ok, this is what I came up with, and it seems to work:

#!/bin/sh
# Simple reboot script by Slink.
# If used to start the Mud, will automatically reboot if
# it goes down.

if [ -r SHUTDOWN.TXT ];
then
rm SHUTDOWN.TXT
./cygma
fi


Ok, I tried something like
shutdown_file=z:/cygwin/cygma/src/SHUTDOWN.TXT
No dice. Not exactly sure how to work this.

Also, is is possible to store the output of a command in a variable?
Such as today=date
echo Reboot time: today

..or something?

I've read all sorts of beginner's tutorials, but some of these little subtleties just don't seem to be addressed. I think I might be in "kindergarten" for a while.
7/30/2008 3:01:33 PM EDT
[#11]
Typically you save the PID of the daemon process you start, so that you can easily check to see if its running, without relying on the process name, which can change or conflict (depending on the string you use to grep).
7/31/2008 5:09:32 PM EDT
[#12]
height=8
Quoted:
....
Also, is is possible to store the output of a command in a variable?
Such as today=date
echo Reboot time: today


Be sure you use back ticks, the one with the tilde on US layouts.

height=8
$ mydate=`date +%Y%m%d%H%M`
$ echo $mydate
200807311806


7/31/2008 5:17:03 PM EDT
[#13]
Also check the following line in your script.


height=8
pgrep cygma || sleep 5;echo -e '\r';date;echo 'Starting reboot script.';echo -e '\r';./cygma


You probably want it to something like this, added parenthesis

height=8
pgrep cygma || (sleep 5;echo -e '\r';date;echo 'Starting reboot script.';echo -e '\r';./cygma)
8/1/2008 5:54:51 AM EDT
[#14]
Parenthesis added, and the variable assignment using ` works great.
Thanks!
8/1/2008 6:07:22 AM EDT
[#15]
Ok, I have just one last problem - backup/restoration of my project. The backup part works the way I have it, actually, but I can't figure out an easy way to restore, and I'm sure I'm probably going about this the wrong way to begin with.

Anyhow, this is what I put in the makefile to back up:
rm -f *.o cygma
rm -f -r /'Copy of Cygma'
cp -r /Cygma /'Copy of Cygma'

Since 'make back' and 'make restore' must be run from the /cygma/src directory where I work, I can't just reverse the process above because I will be in the directory I am trying to delete.

Any suggestions?
8/1/2008 7:29:40 AM EDT
[#16]

Quoted:
Ok, I have just one last problem - backup/restoration of my project. The backup part works the way I have it, actually, but I can't figure out an easy way to restore, and I'm sure I'm probably going about this the wrong way to begin with.

Anyhow, this is what I put in the makefile to back up:
rm -f *.o cygma
rm -f -r /'Copy of Cygma'
cp -r /Cygma /'Copy of Cygma'

Since 'make back' and 'make restore' must be run from the /cygma/src directory where I work, I can't just reverse the process above because I will be in the directory I am trying to delete.

Any suggestions?



Revision control via RCS, CVS, Subversion, etc.  Read up on those.  They let you maintain multiple copies of files and revert to any copy you wish via simple commands, which can be scripted to make them even simpler.

But if you want to stay with just removing and copying, depending on the nature of what you're doing, you could either skip the delete and just copy over everything (this will work fine as long as you wouldn't be leaving 'new' files behind that cause a problem) or you could stick to just deleting files and not the directories.  

'find . -type f -print | xargs rm' would delete all regular files in the current directory and any subdirectories, but would leave directories and other file types intact.

Make sure you don't delete your Makefile!