I'm trying to use crontab to check and re-run a long running script. I've created a script that checks the status and runs the long-running script if needed.

I'm running the long running script with nohup to keep it running when I log out.

The keep-alive script:



write_to_log () {
    date >> ${LOG_PATH}
    echo ${1} >> ${LOG_PATH}
    echo "--------------" >> ${LOG_PATH}

pid=$(pgrep long_script)
 if [ -z "${pid}" ]; then
    write_to_log "long_script is no runnig, starting..."
    nohup ${BASE_PATH}/long_script.py &

crontab entry:

*/30 * * * * /home/user/scripts/keep_alive.sh

When I run the script manually (in bash, ./keep-alive.sh) all works well and the long script starts.

From crontab, the script starts and exits immediately (no issue with the paths/expression), and I see a log written, but the long script stops. So my conclusion is that nohup doesn't work the same for crontab as in bash.

I tired using setsid or disown, but got the same results.

How can I solve it? Thanks.


1 Answer 1


There is no need for nohup when using crontab. Unless your systemd is configured to kill all your processes when you log out there is no interaction between your shell exiting and crontab running processes (or otherwise), and nohup will have no useful effect on that.

Look at your local email (mail or mailx) and read the error messages reported there from cron - or less /var/mail/$USER if you want a shortcut. Alternatively change your crontab line to capture stdout and stderr to a file you can review later

*/30 * * * * /home/user/scripts/keep_alive.sh >ka.log 2>&1

Typically scripts don't run under cron because you've forgotten to set up the environment ($PATH, etc.).

  • Redirecting stdout did the trick :) The issue indeed was environment (the script couldn't find a file), nothing to do with the nohup.
    – Dima.Go
    Commented May 10, 2020 at 15:01

You must log in to answer this question.

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