LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-18-2015, 09:06 AM   #1
CharzelPernell
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Rep: Reputation: Disabled
Smile I can start LFTP at x tme - but can i kill process at x time


Hiya,

I normally read until I find an answer but I am stuck so here goes:

I have unlimited satellite broadband between 00:00 and 06:00am, out of that time I have to be careful so backing up my site needs to happen then - I would like to learn how to do it on the command line.

Here' where I am at:

lftp [my bookmark]

[set local directory]
at:2359
mirror --use-pget-n=5 [remote folder]

I am using a low tech electic timer as a killswitch on my router -
is a command line 'kill switch' possible:

at 0600 kill all ?

Yesterday I though I ended the proces but I had not - must have been a typo - and it carried on and on and on.

I would like to automate it into a script to run periodically eventually - but that is another job.

Thanks
 
Old 03-18-2015, 10:57 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
As you have a fixed start date you could use cron to fire up the backup. And use cron to kill it as well.

crontab -e will fire up a editor to edit the cron file of the active user. Add something like
Quote:
00 0 * * * /home/user/progs/start_backup.sh
59 5 * * * /home/user/progs/end_backup.sh
The start_backup.sh could be kinda like this
Quote:
#!/bin/sh
# save pid of script
echo $$ > /run/backup.pid

# start the backup now
And the end_backup.sh
Quote:
#!/bin/sh
kill $(cat /run/backup.pid)
So the start_backup.sh saves its PID (process identification) intot the file /run/backup.pid and the stop_backup.sh just gets the PID out of the file and kills the script.
Depending on calling on other scripts from start_backup.sh this might not work at first time but should get you started.

You sure can use at. But for doing periodic things cron is better suited by far.
 
1 members found this post helpful.
Old 03-19-2015, 01:08 PM   #3
CharzelPernell
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up Just wat to say thank you...

Quote:
Originally Posted by zhjim View Post
As you have a fixed start date you could use cron to fire up the backup. And use cron to kill it as well.

crontab -e will fire up a editor to edit the cron file of the active user. Add something like


The start_backup.sh could be kinda like this


And the end_backup.sh


So the start_backup.sh saves its PID (process identification) intot the file /run/backup.pid and the stop_backup.sh just gets the PID out of the file and kills the script.
Depending on calling on other scripts from start_backup.sh this might not work at first time but should get you started.

You sure can use at. But for doing periodic things cron is better suited by far.
---------------------------------

Wow - that's really cool and really simple - crontab-e who knew eh!.

Thanks for taking the time for this - this has made life soooo much easier - really great.

Thanks
 
Old 03-24-2015, 05:54 AM   #4
CharzelPernell
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Wink echo $$ outputs a pid that is one less than actual lftp pid?

Quote:
Originally Posted by zhjim View Post
As you have a fixed start date you could use cron to fire up the backup. And use cron to kill it as well.

crontab -e will fire up a editor to edit the cron file of the active user. Add something like


The start_backup.sh could be kinda like this


And the end_backup.sh


So the start_backup.sh saves its PID (process identification) intot the file /run/backup.pid and the stop_backup.sh just gets the PID out of the file and kills the script.
Depending on calling on other scripts from start_backup.sh this might not work at first time but should get you started.

You sure can use at. But for doing periodic things cron is better suited by far.
Hi,

Crontab -e - great - been a revelation - however...

After checking - the pid outputed from my download script:

echo $$ > /run/backup.pid

that is saved is 1 less than the actual lftp process id so when my kill lftp script

kill $(cat /run/backup.pid)

activates at 6.00am it isn't working.

e.g
backup.pid=3443

$ pgrep lftp
3444

How can I get the actual lftp pid or should I make a variable +1 ?

I wish they had taught this in school rather than oxbow lakes

Thanks - Charles
 
Old 03-24-2015, 10:41 AM   #5
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
You would need to kill all childs of your script. Check out this one http://stackoverflow.com/questions/3...hild-processes

PID+1 could work but can cause some real bad things if in the moment from calling lftp from your script another process gets started. You could also try to run lftp in the background using lftp &. The ampersand puts the process into background. Then with echo $! /runt/lftp.pid save the pid of the last process run in background. Just go with the solution in the link above.
 
1 members found this post helpful.
Old 03-26-2015, 12:19 PM   #6
CharzelPernell
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile Thanks zhjim!!! the might pi wakes up at night!

Quote:
Originally Posted by zhjim View Post
You would need to kill all childs of your script. Check out this one http://stackoverflow.com/questions/3...hild-processes

PID+1 could work but can cause some real bad things if in the moment from calling lftp from your script another process gets started. You could also try to run lftp in the background using lftp &. The ampersand puts the process into background. Then with echo $! /runt/lftp.pid save the pid of the last process run in background. Just go with the solution in the link above.
Yesss! Thanks a million Zhjim this is the last piece of the puzzle:

pkill -TERM -P $(cat ~/[path to my backup.pid file] so does
pkill -TERM $(cat ~/[path to my backup.pid file]

Before I go to bed I just shut down openelec on the pi with the remote and leave it to it:
1. auto reboot into Raspian;
2. 11.59 run crontab download script;
3. 05.55 run crontab failsafe kill download script;
4. 06.00 run crontab reboot pi script;
5. @ reboot crontab script creates an autoboot.txt file to make the pi reboot into openelec partition;
6. as openelec starts up - autostart.sh file deletes the autoboot.txt file created;
7. 6.20 little people appear
8. normal operation resumes!

Thanks for all your help. Charles

Last edited by CharzelPernell; 03-26-2015 at 12:20 PM.
 
Old 03-26-2015, 01:36 PM   #7
nelz
LQ Newbie
 
Registered: Aug 2004
Posts: 24

Rep: Reputation: 4
You can kill a process by name, but be careful if there is likely to be more than one running as it will kill them all

killall lftp
 
  


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
Wait for some time Then kill the process... g_paschoal Programming 7 07-26-2014 11:07 AM
Bash Script: Kill a process after fixed time. bruno.miranda Linux - Newbie 1 06-28-2012 03:48 PM
how to kill the process after specific time? samengr Linux - General 5 02-17-2010 12:19 PM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 10:42 AM
script at xwin start to kill old process(es) butthead Programming 3 02-01-2002 09:39 AM


All times are GMT -5. The time now is 03:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration