2

After carefully reviewing the general "cron" answer, I am still stuck.
My crontab reads:

# m h  dom mon dow   command
*/5 * * * * /usr/bin/pkill -f process.py && /usr/bin/python /path/to/process.py > /path/to/process.log 2>&1

I want process.py to be restarted every 5 minutes.
The pkill parts works as expected and process.py is killed every 5 minutes.
However, the second part of my cron task (restarting process.py) does not happen!
- The log file /path/to/process.log does not contain any error, in fact it is not changed.
- running the command outside cron works.
- looking at /var/log/syslog only shows cron executing the command, but no output/errors seem to be logged anywhere
- I tried both ; and && between the two commands. No good.

Any suggestions are welcome!

Shai
  • 121
  • 4
  • @anx yes. I would like to **restart** `process.py` every 5 minutes. – Shai Oct 16 '17 at 11:04
  • 3
    Generally I find that for any logic more complex than running a single command it is better to write a (simple) shell script and have cron execute that. – HBruijn Oct 16 '17 at 11:05
  • Check the permissions on '/path/to/process.log' It sounds like the cron user is not the same one you are testing with - likely the user you test with (root?) owns the log file and the cron process can't write to it. – Brandon Xavier Oct 16 '17 at 11:06
  • @BrandonXavier the log file has the same owner as the crontab. the log has appropriate write permissions. – Shai Oct 16 '17 at 11:09
  • @HBruijn I followed your advice. a script indeed did the trick for me. – Shai Oct 17 '17 at 06:08

1 Answers1

1

Is it possible the pkill is killing the cron process to trigger the new instance of process.py because the regex finds both the running process.py and the cron process which is about to start it?

EightBitTony
  • 9,211
  • 1
  • 32
  • 46
  • oh god please no! if `pkill` is that thorough - what can I do?! – Shai Oct 16 '17 at 11:05
  • When your `process.py` starts, write the pid somewhere, and then have a proper cleanup process which reads the pid and stops only that process (assuming my solution is correct, which is far from proven). – EightBitTony Oct 16 '17 at 11:09
  • Looking at [this answer](https://superuser.com/a/506295/350717) it does not seems like `pkill` is that thorough... – Shai Oct 16 '17 at 11:27
  • 1
    It's not about thoroughness, it's about what processes exist at the point that the `pkill` runs and which ones match the regexp. Try using `-c` and logging the output see if it returns more than 1. – EightBitTony Oct 16 '17 at 13:14