1

I start a script via cron which works fine. But when i then try to end that script via cron (either killall / pkill / pgrep does work) nothing happens.

If i let cron try to kill it or try it my own doesn't do a difference. The only way to stop it seems to be htop and then F9 (sigterm).

So, How to end a cron-started script?

My scripts starts with #!bin/bash and cron starts it by ./path/script.sh.

Any ideas ?

8
  • What about pkill -term?
    – muru
    Dec 23, 2014 at 11:27
  • @muru thanks for the suggestion. But which "terminal" should i name then ? If i understood it right, pkill -t (term[inal]) points on a specific terminal !?? ty
    – tuxaddict
    Dec 23, 2014 at 11:34
  • No, given a signal like SIGKILL, SIGTERM, etc, you can use the name with pkill: pkill -kill, pkill -term, etc.
    – muru
    Dec 23, 2014 at 11:35
  • ty again, but sadly: pkill -term myscript.sh doesn't do the trick either.
    – tuxaddict
    Dec 23, 2014 at 11:43
  • 1
    what should work anyway is killing it by its pid; find its pid with ps -ef, look for the scriptname, then: kill <pid> Dec 23, 2014 at 11:43

1 Answer 1

3

What should work anyway is to kill it by its pid:

  • run ps -ef (or ps -efww if the lines are too long to read the end)
  • find the scriptname, and you will have its pid
  • kill it with:

    kill <pid>
    

    (mind you: not pkill, which is to used as pkill <application>, not as pkill <pid>)

Note:

Using the command:

ps -ef | grep testscript

will list two processes; the one you are looking for and the search process itself that you are running, e.g.:

jacob     4183  3647  0 11:22 pts/10   00:00:00 python3 /home/jacob/Bureaublad/testscript.py
jacob     4202  4189  0 11:22 pts/25   00:00:00 grep --color=auto testscript

which means that you would have to use e.g.:

ps -ef | grep <scriptname> | grep -v grep
> jacob     4183  3647  0 11:22 pts/10   00:00:00 python3 /home/jacob/Bureaublad/testscript.py

To only find the line you need.

Unlike the pgrep command, that filters itself out of the list, as pointed out here: :

pgrep -f testscript
> 4183

That means that using pgrep -f <scrptname>as suggested by @muru is the better way, for more than one reason.

8
  • pgrep is still a much better option than ps | grep.
    – muru
    Dec 23, 2014 at 12:57
  • @muru That may be true, but - as already mentioned - pgrep does not work in this case.
    – tuxaddict
    Dec 23, 2014 at 13:04
  • 1
    @tuxaddict Have you tried pgrep -f?
    – muru
    Dec 23, 2014 at 13:05
  • @muru YES !!! That did the trick ! Thank you muru. Even pkill -f myscript.sh works now :-) I sadly have to say, that finding the PID out and then do a pkill <pid> suprisingly didn't worked either. But the -f option is the solution. :-)
    – tuxaddict
    Dec 23, 2014 at 13:09
  • 1
    @tuxaddict pkill is for use of with the name, kill is to be used with the pid: pkill <application>, kill <pid> Dec 23, 2014 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .