LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Delaying shutdown while cron job is running (http://www.linuxquestions.org/questions/linux-general-1/delaying-shutdown-while-cron-job-is-running-726522/)

bitpicker 05-17-2009 02:52 AM

Delaying shutdown while cron job is running
 
I am trying to set up a local backup on my desktop system using rdiff-backup and an external harddrive. The backup as such is no problem. I would like to run it as a daily cronjob with anachron, which by itself is also no problem. But I don't keep my computer on, wasting energy, when I don't use it. Sometimes it is possible that I only need to actually use it for a couple of minutes for checking e-mail, then shut it down again. That's also why I want to use anachron, there is no specific time when the computer is definitely on.

I believe this might cause problems with rdiff-backup if the backup cron job is running and I issue the shutdown command. If the cron job gets terminated in the middle, rdiff-backup will log errors.

Now I could write a shutdown script which first checks for a backup process, then waits for it to end before shutdown, but I don't want to fiddle with the shutdown sequence. Shutdown is usually initiated by using the logoff button in Xfce, I don't want to do anything fancy to that (if it is even possible) which I'd have to remember if I switch systems, lose it through an update or whatever.

Is there a way to set up a cron job or a script to enable it to tell the system to wait with shutdown until it is finished?

Robin

Tinkster 05-17-2009 04:12 PM

Or you could have two versions (invocations?) of your back-up script;
one that does the back-up, one that shuts the machine down after
back-up completed. No farting around with checks, cron-jobs or timers.
You choose what it's gonna do before you start it.



Cheers,
Tink

bitpicker 05-18-2009 05:07 AM

That would mean I have to invoke it manually, which is what I am doing now. I want the backup to be a background process managed by anachron so I don't have to start it or mind it.

I have since looked into trapping signals, but there seems to be no way to trap all the signals, sooner or later the shutdown process will issue a kill command and the backup will be stopped ungracefully.

I could write a shutdown script which will only actually shutdown or reboot the system if no backup task is running, but I still wish there was a better way.

Robin

Tinkster 05-18-2009 05:58 AM

If you just add at the end of your back-up script:
halt
...

Nothing manual about it.


Cheers,
Tink

bitpicker 05-18-2009 08:58 AM

You are misunderstanding me; I don't want a backup script which runs then shuts down the system, that's trivial, I want a backup script which gets started automatically as a cron job and will delay a user-initiated shutdown of the system until it is finished.

Robin

pcunix 05-18-2009 03:13 PM

Quote:

Originally Posted by bitpicker (Post 3544598)
You are misunderstanding me; I don't want a backup script which runs then shuts down the system, that's trivial, I want a backup script which gets started automatically as a cron job and will delay a user-initiated shutdown of the system until it is finished.

Robin

You need to modify your shutdown to pay attention to the backup. That could be as simple as having your backup script create a sentinel file while it runs; the modified shutdown script would look for that file and hold off while it still exists.

bitpicker 05-19-2009 07:25 AM

OK, if modifying the shutdown script is the only way of doing it, I'll have a look into it. Thanks for your input.

Robin

colucix 05-19-2009 07:35 AM

You can simply check for the existence of the process and sleep some minute until the process terminates:
Code:

while ps -C backup_command_name > /dev/null
do
  # backup process is still running... sleep 5 minutes
  sleep 300
done
/sbin/shutdown -h now



All times are GMT -5. The time now is 12:50 AM.