22
  1. How can I see crontab tasks which are in progress?
  2. How can I stop crontab tasks which are in progress, manually?

Is [this] question about see the current running cron processes?

Yes

To kill a process manually one way is to use pkill

I had thought about pkill/killall, but some of my commands in crontab file are respectively! it means that after finishing a command, the next one will be started! for example:

sudo crontab -e
00 10 * * * /usr/bin/wget LINK ; shutdown -h now

So, If I kill wget, the computer will be powered off!!! -> I want to kill a cron task fully, not a part of it!

The next pkill problem: What about scripts? (I've imported some bash scripts to crontab -e) --> sudo pkill ???

  • Is your question about see the current running cron processes? or the list of cron-jobs? Please edit your question to be more clear about this. To stop the running cron-job go (askubuntu.com/q/272419/66980) – Mohammad Etemaddar Jun 26 '13 at 18:16
  • Edited; 1. yes, watching current running cron processes? (I want to bring processes to foreground); 2. Then, I want to stop it manually?! (not from crontab schedule) – mini Jun 26 '13 at 19:13
  • The best way to add additional information to your question is by editing it, with the edit button. It is better visible that way, and comments are mainly for secondary, temporary purposes. Comments are removed under a variety of circumstances. Anything important to your question should be in the question itself. – guntbert Jun 26 '13 at 19:47
48
+50

Firstly, use only one command per line in crontab. Change this crontab line:

00 10 * * * /usr/bin/wget LINK ; shutdown -h now

so it looks like:

00 10 * * * /path/to/my/crontab/script1.sh

and create /path/to/my/crontab/script1.sh with this content:

#!/bin/bash
/usr/bin/wget LINK
shutdown -h now

Of course, don't forget to give it execution permission:

chmod +x /path/to/my/crontab/script1.sh

Secondly, you can see running crontab tasks, in a useful and readable format, in the output of:

ps -o pid,sess,cmd afx | egrep -A20 "( |/)cron( -f)?$"

They will appear in the first lines, something like this:

1108  1108 cron
4288  1108 \_ CRON
4289  4289     \_ /bin/sh -c /path/to/my/crontab/script1.sh
4290  4289         \_ /bin/bash /path/to/my/crontab/script1.sh
4295  4289             \_ /usr/bin/wget LINK

First column is PID, second is Session ID and third is the command started by cron. You can kill all the processes related to a specific cron task using the Session ID, so in the example above you should kill Session ID 4289:

pkill -s 4289
| improve this answer | |
  • 1
    I get the this when running the "running crontab tasks" command: sh ps: cmd: keyword not found ps: illegal argument: afx – AlecRust Jul 24 '16 at 16:24
  • so cron doesn't give an "handy" way for show his processes? – Cirelli94 Sep 29 '16 at 14:52
  • @alecrust: Try this ps -o pid,sess,cmd -afx | egrep "( |/)cron( -f)?$" – Bulrush Oct 23 '17 at 16:07
0

To kill a process manually one way is to use pkill. pkill will stop all processes that have the following pattern in its name. for example to stop all wget processes use:

sudo pkill wget
| improve this answer | |
  • I had thought about pkill/killall, but some of my commands in crontab file are respectively! it means that after finishing a command, the next one will be started! for example: sudo crontab -e => 00 10 * * * /usr/bin/wget LINK ; shutdown -h now => So, If I kill wget, the computer will be powered off!!! -> I want to kill a cron task fully, not a part of it! the next pkill problem: What about scripts? (I've imported some bash scripts to crontab -e) - > sudo pkill ??? – mini Jun 26 '13 at 19:47
0

change your cronjob like this

00 10 * * * /usr/bin/wget LINK && shutdown -h now

This should work, because the && means that the second command only gets executed, when the first one was finished correctly (returning status 0 to the console/system), which shouldn't be the case (because the process got killed by the user and it should return another value then 0).

| improve this answer | |

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.