LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 11-20-2008, 04:12 AM   #1
pirhana
LQ Newbie
 
Registered: Nov 2007
Location: UK
Distribution: CentOS
Posts: 20

Rep: Reputation: 0
Shell script help


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?

Any help is gratefully appreciated.
 
Old 11-20-2008, 04:41 AM   #2
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Devuan
Posts: 3,654
Blog Entries: 33

Rep: Reputation: 283Reputation: 283Reputation: 283
Hi, to shutdown your system...halt

shutdown -h now

to reboot...

shutdown -r now

Hope this helps.

Regards Glenn
 
Old 11-20-2008, 05:24 AM   #3
pirhana
LQ Newbie
 
Registered: Nov 2007
Location: UK
Distribution: CentOS
Posts: 20

Original Poster
Rep: Reputation: 0
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?
 
Old 11-20-2008, 05:51 AM   #4
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
I think you can run another job at 8am. That job should have a simple script that:

does something like

ps -ef|grep <your_script_name>|grep -v grep|awk '{ print $2 }'|xargs kill
 
Old 11-20-2008, 09:58 AM   #5
pirhana
LQ Newbie
 
Registered: Nov 2007
Location: UK
Distribution: CentOS
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rizwanrafique View Post
I think you can run another job at 8am. That job should have a simple script that:

does something like

ps -ef|grep <your_script_name>|grep -v grep|awk '{ print $2 }'|xargs kill
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
 
Old 11-20-2008, 01:10 PM   #6
pirhana
LQ Newbie
 
Registered: Nov 2007
Location: UK
Distribution: CentOS
Posts: 20

Original Poster
Rep: Reputation: 0
rizwanrafique

Thank you for your code. It works fine from the command line and also in a script:
Quote:
Originally Posted by rizwanrafique View Post
#!/bin/sh
ps -ef|grep rsync.sh|grep -v grep|awk '{ print $2 }'|xargs kill
but when I tried to modify it to accept any script name from the command line, it fails. It simply says "Terminated" and the script continues to run:
Code:
#!/bin/sh

if [ $# -eq 1 ]; then
   ps -ef|grep $1|grep -v grep|awk '{ print $2 }'|xargs kill
fi
I'm calling it by entering:
./killprocess.sh rsync.sh
where killprocess.sh contains the code above and rsync.sh is running and needs to be terminated

As I said, I'm still new to this but learning fast. Apologies if I have made a school boy error
 
Old 11-28-2008, 04:49 AM   #7
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
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:

Code:
PPID=`ps -ef|grep <your_script>|grep -v grep|awk '{print $2}'`
CPID=`ps -ef|grep $PPID|grep -v grep|awk '{print $2}'|grep -v $PPID`
Now all you need to do is to kill both of them.

Second part of your original question:

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 :-)
 
Old 11-28-2008, 04:22 PM   #8
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Devuan
Posts: 3,654
Blog Entries: 33

Rep: Reputation: 283Reputation: 283Reputation: 283
Hi, you might find these articles of use,

Jerry Peek has these articles on his own page too, available in .pdf format, ten (10) in all.

Linux Magazine, "Here’s the start of a series on little-known topics that wizards should know. Knowledge of the Harry Potter series not required."

Jerry Peek's
Wizard Boot Camp, Part One

http://www.linux-mag.com/id/4038

You guys are way ahead of me, but I just thought I'd share the resource, it covers a wide variety of things. I'm still working on it.

Cheers, Glenn

Last edited by GlennsPref; 11-28-2008 at 04:26 PM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:12 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration