LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 08-30-2005, 09:37 PM   #1
Red Squirrel
Member
 
Registered: Dec 2003
Distribution: FC9 on main server
Posts: 753

Rep: Reputation: 36
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.

Last edited by Red Squirrel; 08-30-2005 at 09:38 PM.
 
Old 08-30-2005, 09:58 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,476

Rep: Reputation: 156Reputation: 156
"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
 
Old 08-30-2005, 10:03 PM   #3
Red Squirrel
Member
 
Registered: Dec 2003
Distribution: FC9 on main server
Posts: 753

Original Poster
Rep: Reputation: 36
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.
 
Old 08-31-2005, 09:45 AM   #4
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,476

Rep: Reputation: 156Reputation: 156
"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

Last edited by jailbait; 08-31-2005 at 09:47 AM.
 
Old 08-31-2005, 01:00 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

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


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SUID problem running cron jobs? mitchloft Fedora 4 03-24-2005 12:30 PM
cron jobs working2hard Linux - Software 0 07-28-2004 05:31 PM
cron jobs not running scarr3d Linux - Software 7 05-25-2004 04:21 PM
CRON Jobs scottpioso Linux - Software 63 11-28-2003 06:45 PM
cron jobs Zyanid Linux - Software 0 08-27-2002 05:10 AM


All times are GMT -5. The time now is 11:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration