sergiod
sergiod

Reputation: 295

Custom shell script in crontab

I've a simple shell script that executes a docker-exec command inside a container. The script is located in /var/www/mysite-nginx/nginx-reload.sh and permissions of this file are -rwxrwxr-x

#!/bin/sh
docker exec -it mysite_nginx  nginx -s reload

If I execute this script directly from shell, it works. But if I add the script to my crontab with the following line, it doesn't work.

15 4 * * * /var/www/mysite-nginx/nginx-reload.sh

I suppose that cron doesn't execute the command, or what is wrong?

On /var/log/syslog I have:

Jul 23 15:30:01 arrubiu CRON[29511]: (sergej) CMD (/var/www/mysite-nginx/nginx-reload.sh)

[EDIT] Solved in this way: docker exec is not working in cron

Upvotes: 1

Views: 163

Answers (1)

B--rian
B--rian

Reputation: 5880

The issue seems to be that docker is not found. There are two ways around:

  1. You enter the full paths of all application in your crontab script, you can find that out using e.g. locate docker, so that it looks something like
#!/bin/sh
/usr/bin/docker exec -it mysite_nginx  
/usr/bin/nginx -s reload
  1. Alternatively, you can set the $PATH and other environment variables in the same way how they are set for a usual sh-script. To achieve that, first backup what is saved in /etc/environment, and then flush it with the currently available variables by executing:
cp /etc/environment > ~/my_etc_environment_backup
env >> /etc/environment

Related questions on SO

Upvotes: 1

Related Questions