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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.