LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I can start LFTP at x tme - but can i kill process at x time (https://www.linuxquestions.org/questions/linux-newbie-8/i-can-start-lftp-at-x-tme-but-can-i-kill-process-at-x-time-4175537116/)

CharzelPernell 03-18-2015 08:06 AM

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

zhjim 03-18-2015 09:57 AM

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.

CharzelPernell 03-19-2015 12:08 PM

Just wat to say thank you...
 
Quote:

Originally Posted by zhjim (Post 5334082)
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

CharzelPernell 03-24-2015 04:54 AM

echo $$ outputs a pid that is one less than actual lftp pid?
 
Quote:

Originally Posted by zhjim (Post 5334082)
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

zhjim 03-24-2015 09:41 AM

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.

CharzelPernell 03-26-2015 11:19 AM

Thanks zhjim!!! the might pi wakes up at night!
 
Quote:

Originally Posted by zhjim (Post 5336986)
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 :hattip:

nelz 03-26-2015 12:36 PM

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


All times are GMT -5. The time now is 01:17 AM.