Ubuntu – Cron is executed but script doesn’t work

cronPHPscripts

I know there are a lot of similar question and I've tried lots of things but still can't make it work.

I have cronjob which is scheduled to run on 10min. I can see in /var/log/syslog that it is run normally

Oct 21 07:30:01 stan CRON[7604]: (stan) CMD (stan /home/stan/update.sh)
Oct 21 07:40:01 stan CRON[8304]: (stan) CMD (stan /home/stan/update.sh)
Oct 21 07:50:01 stan CRON[8751]: (stan) CMD (stan /home/stan/update.sh)
Oct 21 08:00:01 stan CRON[9347]: (stan) CMD (stan /home/stan/update.sh)
Oct 21 08:10:01 stan CRON[9789]: (stan) CMD (stan /home/stan/update.sh)

update.sh call php script which update database. When I run directly from terminal the shell script database got updated and it's working perfectly

./update.sh

But from cronjob doesn't update database. My cron

*/10 * * * * stan /home/stan/update.sh

Command produced from shell script is

/usr/bin/php /var/www/html/site/update.php

Permissions of both files

-rwxrwxr-x  1 stan stan    123 Oct 20 15:09 update.sh
-rwxr-xr-x  1 stan www-data 1301 Oct 21 07:52 /var/www/html/site/update.php

Any idea what can be the problem?

Update: PATH

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

update.sh

$ cat update.sh 
#!/bin/sh

list="/var/www/html/site"
config="/usr/bin/php"

for i in "$list"
do
    "$config" "$i"/update.php
done

Reference my question from yesterday which was about the shell and php now is about cron.

$ whereis php

php: /usr/bin/php5.6 /usr/bin/php /usr/lib/php /etc/php /usr/include/php /usr/share/php5.6-intl /usr/share/php7.0-mbstring /usr/share/php7.0-common /usr/share/php5.6-curl /usr/share/php5.6-gd /usr/share/php5.6-mcrypt /usr/share/php5.6-common /usr/share/php5.6-readline /usr/share/php5.6-json /usr/share/php /usr/share/php5.5-mbstring /usr/share/php5.6-opcache /usr/share/php5.6-mbstring /usr/share/php5.6-xml /usr/share/php5.5-common /usr/share/php5.6-mysql /usr/share/man/man1/php.1.gz

$ which php
/usr/bin/php

Best Answer

You seem to be confusing two different methods of invoking cron jobs.

Ubuntu inherits from Debian a somewhat confusing policy of supporting both user crontabs that are stored in a spool area /var/spool/cron, and system-wide cron jobs run from /etc/crontab and the files in /etc/cron.d.

Jobs specified in /etc/crontab or via files in /etc/cron.d need an extra field in order to allow them to be run as a different user so the format is something like

*/10 * * * * <username> <command> <args>

Jobs set up via the spool area using crontab -e (or sudo crontab -e for root) already belong to a specific user, and don't need the user field

*/10 * * * * <command> <args>

If you include the username field in a cron job set up via a crontab -e command, it will be misinterpreted as a command: as we can see from your log output,

Oct 21 07:30:01 stan CRON[7604]: (stan) CMD (stan /home/stan/update.sh)

cron is interpreting stan as a command with argument /home/stan/update.sh

The solution should be simply to remove the username stan from your crontab.

Related Question