Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
I have managed to write a script to backup my data to a remote server using rsync which works well. I'm very pleased with my achievements as I'm still new to this.
I run the script every night by cron as I get free bandwidth between midnight and 8am
I am now trying to get the script to shutdown the machine when it has finished, but simply adding "shutdown now" doesn't work as the system just hangs (tested from command line)
I am also wondering if it is possible to automatically kill the script at 8am if the backup has not finished, thus saving my paid-for bandwidth and leaving the rest of the backup until the next evening?
Thank you, that works perfectly. Taking this a stage further, is there any way to test if the rsync finished or was stopped?
For example, when I run it from the command line, I sometimes need to end the script as it consumes my bandwidth, pressing Ctrl-C to terminate it now results in the shutdown
I have heard of exit codes in other scripting languages, is this available here?
That is amazing code. I followed it through, section by section to understand what was happening each time (with help from the man pages) and I think it is brilliant. It works perfectly.
I'm just left with one part of my code to complete. Has anyone got any idea about the test on rsync to see if it finished naturally or if the script was terminated part way through? I only want to shutdown the machine if the script was run via cron and rsync completed. I don't want the machine shutting down when the second cron terminates the rsync script at 8am
hmm when a script calls another script (or program) that script (or program) runs as the child for calling script. You need to kill both the parent and child manually.
I'm sure there are better ways finding out both parent and child process ids but one way is:
I am not aware of any way to play with return codes to see if the rsync process has finished or not but we can implement that manually quite easily. This is how:
- create a lock fine before starting rsync in your script.
- when rsync finishes delete that file.
You can run a cron job to see if that file exists at 8am or not. If it does kill rsync process and the parent process. Delete the file. And do not shutdown. If the file doesn't exist just shutdown the machine.
Or just run a shutdown command after rsync in your script to shut it down straight after. And at 8am if the computer is still running then kill the rsync and the parent process :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.