Posted: 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. |
Replace the echo with whatever command you want it to do and replace the sshd with whatever process you are looking for.
|
|
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 |
|
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. |
|
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> |
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. |
Also check the following line in your script.
You probably want it to something like this, added parenthesis
|
|
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! |