Skip to main content
edited body
Source Link
Dima.Go
  • 123
  • 1
  • 1
  • 6

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:

#!/bin/bash BASE_PATH=~/scripts LOG_PATH=${BASE_PATH}/keep_alive.log 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 & fi 

crontancrontab 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.

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:

#!/bin/bash BASE_PATH=~/scripts LOG_PATH=${BASE_PATH}/keep_alive.log 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 & fi 

crontan 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.

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:

#!/bin/bash BASE_PATH=~/scripts LOG_PATH=${BASE_PATH}/keep_alive.log 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 & fi 

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.

Source Link
Dima.Go
  • 123
  • 1
  • 1
  • 6

Use crontab to invoke nohup in script

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:

#!/bin/bash BASE_PATH=~/scripts LOG_PATH=${BASE_PATH}/keep_alive.log 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 & fi 

crontan 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.