1

Here is my crontab:

42 17 * * 1-5 /bin/pkill -f 'MyExecutable.exe' ;  touch /tmp/here.log

And according to /var/log/cron it did run:

May 22 17:42:01 server1 CROND[326732]: (myuser) CMD (/bin/pkill -f 'MyExecutable.exe' ;  touch /tmp/here.log)

And the process of MyExecutable.exe did die. However, here.log wasn't created in /tmp.

If I manually run it under myuser:

touch /tmp/here.log

the file is created successfully (hence, not permission issue for sure). I even tried to move touch into a separate cronjob in crontab, and it worked.

Why doesn't touch after the semicolon run?

5
  • Is it possible that touch isn't in the default crontab path (/usr/bin)?
    – harrymc
    Commented May 22, 2018 at 10:09
  • Nah, it is there. I even tried to move touch into a separate cronjob in crontab, and it worked.
    – HCSF
    Commented May 22, 2018 at 10:32
  • What happens if you enclose the two commands in parenthesis ?
    – harrymc
    Commented May 22, 2018 at 12:51
  • like 42 17 * * 1-5 (/bin/pkill -f 'MyExecutable.exe' ; touch /tmp/here.log)?
    – HCSF
    Commented May 22, 2018 at 14:50
  • Yes, try it like that.
    – harrymc
    Commented May 22, 2018 at 16:17

2 Answers 2

3

This question relates with this one:

Running two commands sequentially in a cron job?

You should separate the two commands with &&, instead of ;

1
  • 1
    I believe that && is used if the user really wants to run the second command after the first one is success. However, a user might want to run the 2nd command regardless whether the first command is success.
    – HCSF
    Commented May 22, 2018 at 14:49
3

This can be replicated by

sh -c 'pkill -f "MyExecutable.exe" ; touch /tmp/here.log'

Cron runs your command by passing it to a shell (sh or other), so effectively it's very similar to the above line. Regardless of whether any MyExecutable.exe runs or not, pkill -f matches (also) the shell and kills it before it runs touch. This is because

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

(source: man 1 pkill)

Possible solutions:

  1. If you don't need -f, just drop it:

    42 17 * * 1-5 /bin/pkill 'MyExecutable.exe' ;  touch /tmp/here.log
    
  2. If you do need -f, run touch before pkill:

    42 17 * * 1-5 touch /tmp/here.log ; /bin/pkill -f 'MyExecutable.exe'
    

You must log in to answer this question.

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