1

I need to make Linux Ubuntu platform automatically start and kill one process, for example, if time is 8am process starts, if time is 7pm process is killed, and that have to be every day, and should be quite okay, if time interval will be easy to change.

I'm trying to use simple codes in crontab:

28 12 * * * /home/pi/Desktop/start.sh 50 11 * * * pkill led.py 

Don't look at times, I've tried to change them, start.sh starts led.py script, but I cant stop it if I'm using pkill -9 -f led.py. The process gets killed, but LEDs won't turn off. If I manually start the program and then kill it with Ctrl+c, the LEDs turn off. Where is the problem? Why I can't kill the process and at the same time turn off the LEDs?

12
  • What does the "manual kill" step, which successfully turns the LEDs off, involve? Ctrl-C? Another kill/pkill command? Commented Sep 27, 2016 at 10:04
  • Ctrl+C at this time, kill/pkill won't work at all Commented Sep 27, 2016 at 10:05
  • Ctrl-c sends a different signal -- try "kill -2" Commented Sep 27, 2016 at 10:08
  • kill -2 led.py like this? Commented Sep 27, 2016 at 10:09
  • 1
    No, kill -2 pid, where pid is the process id. Or pkill -2 led.py. Commented Sep 27, 2016 at 10:10

2 Answers 2

2

When you type Ctrl+c, that usually sends the process an "INT" signal. From signal(7):

 Signal Value Action Comment ────────────────────────────────────────────────────────────────────── ... SIGINT 2 Term Interrupt from keyboard 

It is common for processes to install a handler for this signal, allowing them to perform some cleanup before exiting. In the case of your led.py script, it sounds like this handler turned off the LEDs.

By default, pkill and kill send the "TERM" (15) signal. (You also tried sending "KILL" (9).) These signals caused led.py to die less gracefully, without getting a chance to run its finishing-up function.

To allow led.py to finish cleanly, you should send the "INT" (2) signal, with

pkill -2 [process specifier] 

The pkill command in your crontab may also have been failing to find the process, because the name you gave wasn't the one it was searching for. From pkill(1):

-f, --full

The pattern is normally only matched against the process name. When -f is set, the full command line is used.

As your script, led.py, was presumably a python script, the process name was simply python (or python3, or similar). The full command line would be something like python led.py, so the -f options lets you match on that.

pkill -2 -f led.py 
0

I use this function from many years ago:

function killit () { for process in "$@"; do kill -0 $process &>/dev/null if [[ $? == 0 ]] ; then sudo kill $process #(sends a TERM, wait 5 seconds) sleep 5 RUNNING=$(ps aux | tr -s " " "\t" | cut -f 2 | grep $process) if [[ $RUNNING ]] ; then echo "$0 WARNING: process $process still running, trying kill again" sudo kill $process #(yes, try again, wait 5 seconds) sleep 5 RUNNING=$(ps aux | tr -s " " "\t" | cut -f 2 | grep $process) if [[ $RUNNING ]] ; then echo "$0 WARNING: process $process still running, trying kill -INT" sudo kill -INT $process #(wait for it) sleep 5 RUNNING=$(ps aux | tr -s " " "\t" | cut -f 2 | grep $process) if [[ $RUNNING ]] ; then echo "$0 WARNING: process $process still running, trying kill -INT again" sudo kill -INT $process #(damn, still not dead?) sleep 5 RUNNING=$(ps aux | tr -s " " "\t" | cut -f 2 | grep $process) if [[ $RUNNING ]] ; then echo "$0 WARNING: process $process still running, trying kill -KILL" sudo kill -KILL $process #(same thing as -9) sleep 5 RUNNING=$(ps aux | tr -s " " "\t" | cut -f 2 | grep $process) if [[ $RUNNING ]] ; then echo "$0 WARNING: process $process still running, trying kill -KILL again" sudo kill -KILL $process #(something is wrong) sleep 5 RUNNING=$(ps aux | tr -s " " "\t" | cut -f 2 | grep $process) if [[ $RUNNING ]] ; then echo "$0 WARNING: Can't kill process $process" logger "$0 WARNING: Can't kill process $process" fi fi fi fi fi fi fi done } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.