Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Mint 20.1 on workstation, Debian 11 on servers
Posts: 1,327
Rep:
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.
"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.
Distribution: Mint 20.1 on workstation, Debian 11 on servers
Posts: 1,327
Original Poster
Rep:
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.
"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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.