LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   shutdown only if no cron jobs are running (http://www.linuxquestions.org/questions/linux-software-2/shutdown-only-if-no-cron-jobs-are-running-358674/)

Red Squirrel 08-30-2005 10:37 PM

shutdown only if no cron jobs are running
 
I have a backup server that I'm setting up, and this server will only be turned on to perform the backups then shut down. So I want to run a shut down at the end of the cron scripts, but depending on the day, there may be multiple scripts running at once so when it gets to the shutdown command, I want to check if another script is running and if yes, it just skips the shut down, since the last remaining script will do the shut down. I can't seem to figure out a way of doing this.

Easiest way is probably checking if the load is lower then 0.5 or something.

I was thinking of having scripts create a file and check if the file exists, but when the first script ends and deletes the file, it will do it regardless of if the other script is running or not, so this is not really a good solution unless I have 1 file per script, but this can get VERY messy since I'd have to code it in every single script to create a seperate file + check for each one. So if I add 1 script I have to edit all of them.

So going by load is probably the easiest, just not sure how to go about that.

Another way I thought of is that backups can create a unique file inside a folder then delete it after, so before shutting down I can check if the folder is empty. But that too, I'm really not sure how I'd do that.

jailbait 08-30-2005 10:58 PM

"Another way I thought of is that backups can create a unique file inside a folder then delete it after, so before shutting down I can check if the folder is empty. But that too, I'm really not sure how I'd do that."

What you are describing is very similar to the lock mechanism. The way that I would do it is for all the cron processes to create a shared lock on some lock file using the flock command. Each cron process would remove the lock just before they exit. The exception is the shutdown job. The shutdown job starts last (by cron time) and creates an exclusive, blocking lock on the lock file. Thus the shutdown job does not return from the flock command until all other processes locking on the same lock have finished. Thus when the shutdown job returns from the flock command it knows that it is the only cron shutdown process still running. (The shutdown job must unlock its lock before it shuts down or the next shutdown after reboot will be a non-event.)

"I want to check if another script is running and if yes, it just skips the shut down,"

The way that I describe the solution, only one script will be the designated shutdown job. The other cron jobs that issue shared lock requests will never attempt a shutdown.

See:
man flock

http://linux.about.com/library/cmd/blcmdl2_flock.htm

----------------------------
Steve Stites

Red Squirrel 08-30-2005 11:03 PM

hmm the flock command does not work for me, so is this like a C++ function? so I'd have to design a custom application to do this?

Really the easiest way is to check if a certain folder is empty, so is there a way to do that with bash? since each script can create it's own file in that folder and delete it after execution, so if no scripts are running then that folder is empty.

jailbait 08-31-2005 10:45 AM

"hmm the flock command does not work for me, so is this like a C++ function?"

flock is also a bash command. The lock mechanism is available in every language.

"so I'd have to design a custom application to do this?"

No. use flock as part of your scripts.

"Really the easiest way is to check if a certain folder is empty, so is there a way to do that with bash? since each script can create it's own file in that folder and delete it after execution, so if no scripts are running then that folder is empty."

Except that you have the problems that the scripts are running as parallel and the timing can get screwed up. Using the kernel's lock mechanism keeps the multithreading problems straight.

--------------------------
Steve Stites

Tinkster 08-31-2005 02:00 PM

One correction: flock is not a bash-command, it's part
of the util-linux package.


Cheers,
Tink


All times are GMT -5. The time now is 01:28 AM.