I have 3 Debian servers. For years I was using this command to run a php cronjob:
19,56 * * * * /usr/bin/php /home/sites/cron.php >/dev/null 2>&1
it works on my first server. On the second server it doesn't work and I use:
19,56 * * * * php -f /home/sites/cron.php >/dev/null 2>&1
on the third server don't work any commands. I tried:
19,56 * * * * /usr/bin/php /home/sites/cron.php >/dev/null 2>&1
19,56 * * * * /usr/bin/php -f /home/sites/cron.php >/dev/null 2>&1
19,56 * * * * /usr/bin/php -q -f /home/sites/cron.php >/dev/null 2>&1
19,56 * * * * php -f /home/sites/cron.php >/dev/null 2>&1
19,56 * * * * php -q -f /home/sites/cron.php >/dev/null 2>&1
19,56 * * * * php /home/sites/cron.php >/dev/null 2>&1
whereis php
php: /usr/bin/php /usr/share/man/man1/php.1.gz
19,56 * * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php
I am getting crazy. Never in my life had problems like this. Any ideas how to set up a cronjob? Thanks.
Please post the relevant error messages. Reschedule the cron job like this:
19,56 * * * * /usr/bin/php /home/sites/cron.php > /home/<user>/cron.err 2>&1
Look what is present in the file /home//cron.err. Or simply run the command in a terminal
/usr/bin/php /home/sites/cron.php
and verify if it is working.
Related
I tried the below command and crontab stopped running any jobs:
echo "#reboot /bin/echo 'test' > /home/user/test.sh"| crontab -
What is the correct way to script adding a job to crontab in linux?
I suggest you read Cron and Crontab usage and examples .
And you can run this:
➜ ( printf -- '0 4 8-14 * * test $(date +\%u) -eq 7 && echo "2nd Sunday"' ) | crontab
➜ crontab -l
0 4 8-14 * * test $(date +\0) -eq 7 && echo "2nd Sunday"
Or
#!/bin/bash
cronjob="* * * * * /path/to/command"
(crontab -u userhere -l; echo "$cronjob" ) | crontab -u userhere -
Hope this helps.
Late answer, but on CentOS I create a new cronjob (for root, change user as needed) from a bash script using:
echo "#reboot command..." >> /var/spool/cron/root
>> will force appending to existing cronjobs or create a new cronjob file and append to it if it doesn't exist.
Im not sure about this
but try this one
echo "* * * * * whatever" > /etc/crontabs/root
then check the "crontab -e" you will see your command there
For those who are using alpaine distribution , do not forget to call "crond" to make your crons start
I've installed motion mmal on Raspberry Pi and its recording video whenever I run startmotion script:
#!/bin/sh
nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &
However, Its making continuous video until I stop motion.
I want to make a video of 5 minute after every 10 minutes. I tried the timeout command:
#!/bin/sh
timeout 5m nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &
I was able to run script after every 1 hour by using this code:
0 * * * * nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion- mmalcam.conf 1>/dev/null 2>&1 </dev/null &
5 * * * * pkill -9 motion
How can I run this script after every 10 minutes?
Did the timeout option work, though?..
Anyway, if you want to run the cron commands you listed every 10 minutes, then this should work:
*/10 * * * * nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &
5-59/10 * * * * pkill -9 motion
You set the "step" after the /, default step is 1, and * is equivalent to "range" 0-59 for minutes. See https://stackoverflow.com/a/19204734/1375470 for a great explanation.
By the way, if timeout command works, it would look better (aesthetically) in cron, as a single command at */10 IMO.
I have a remote .sh script to take backup of mySQL file. Cron is running by .sh file not executing. Content of Crontab are:
#SHELL=/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/db.output
File Contents are:
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M)
zipname="db_prod_$DATE.sql"
mysqldump db_prod > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname
zipname="db_staging_$DATE.sql"
mysqldump db_staging > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname
Bash path
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
I am on Ubuntu.
File is is in +x mode.
#!/usr/bin/bash
DATE=$(date +%Y%m%d_%H%M)
zipname="db_prod_$DATE.sql"
SCRIPT_LOG_FILE="/tmp/backup.log"
echo "[${DATE}] [INFO] Starting personnal backup" > ${SCRIPT_LOG_FILE}
$(which mysqldump) db_prod > $zipname
if [ "$?" -eq "0" ] && [ -f ${zipname} ];then
echo "[$(date +%Y%m%d_%H%M)] [INFO] Mysqldump save file ${zipname}" > ${SCRIPT_LOG_FILE}
# file is created continue job with this file ...
$(which aws) s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname
else
echo "[$(date +%Y%m%d_%H%M)] [ERROR] Mysqldump can't save file ${zipname}" > ${SCRIPT_LOG_FILE}
fi
exit 0
And crontab -l must be:
* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output
check cron error:
try to see error with:
~$ sudo cat /var/log/syslog|grep CRON|grep db_backup.sh
~$ sudo cat /var/log/syslog|grep CRON|grep MY_USERNAME
(Permission denied or other)
where MY_USERNAME is the user execute : crontab -e
check script error:
* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/result.db.output
* * * * * /home/ubuntu/dscript/db_backup.sh 2> /tmp/error.db.output
or simply
* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output
The question is whether the error comes from the execution of cron or is in the script, the above methods will help you find the source of the problem.
my goal is to save cronjob error and email it to me. I don't care about standard output, that's why I redirect it to /dev/null.
Sending mail is done by this setting in crontab:
MAILTO=my#email.com
I tried to run it with following command:
* * * * * /path/to/script.sh > /dev/null 2 >> /path/to/file.log
It emails the error all right. The log file is created but is empty.
What am I doing wrong?
==================================================================================
I found the solution. Thank you all for your help! (I cannot post it as an answer, so I am amending the solution here.)
I used this reference How can I redirect stderr to a pipe?
# version 2: redirect stderr to the pipe without getting stdout (it's
# redirected to /dev/null)
myprog 2>&1 >/dev/null | grep ...
My solution is:
#crontab
MAILTO=my#email.com
* * * * * /path/to/script.sh 2>&1 >/dev/null | tee -a /path/to/file.log
Explanation: redirecting stderr to the pipe without getting stdout and then using tee -a for appending the stderr to the log file and printing it to the terminal, which is redirected automatically to email (see MAILTO).
So I have a log file and email both containing the error message.
My solution is:
#crontab
MAILTO=my#email.com
* * * * * /path/to/script.sh 2>&1 >/dev/null | tee -a /path/to/file.log
Explanation see above in my first post.
try this , it works
* * * * * script.sh >/dev/null 2>> /path/to/file.log || echo "error check log file"
I have cron
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash /root/Dropbox/1.sh'
my 1.sh
#!/bin/bash -l
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
x=1
while [ $x -le 3 ]
do
URL=$(head -n 1 /root/Dropbox/query.txt)
lynx -dump $URL | egrep '^http' > /root/Dropbox/urls.txt
x=$(( $x + 1 ))
done
Bash as default
# echo $SHELL
/bin/bash
Why cron doesn't run 1.sh ?
remove "root" in line crontab line:
* * * * * /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash /root/Dropbox/1.sh'
If you need root rights put in crontab of root user.
With "root" you will also find error in syslog or messages logs.
And of course be sure the cron daemon is running: ps -ef | grep cron
ADD:
I've tested it with simple touch a file (on ubuntu):
contab line:
* * * * * /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash ~/1.sh'
1.sh:
#!/bin/bash
touch /tmp/hallo
ADD: (looking at lynx command)
A version of 1.sh Script it works for me.
#!/bin/bash
x=1
while [ $x -le 3 ]
do
URL="http://www.subir.com/lynx.html"
lynx -dump $URL | egrep '.*\. http' > urls.txt
x=$(( $x + 1 ))
done
I changed the regEx on egrep. Your output of lynx may be different (other version of lynx). And I used a fixed test URL (lynx man page). The urls.txt will be filled. Script is triggered by cron. The while will have no effect noting in loop logic will be change on next run.
stephan#coppi:~$ more urls.txt
1. http://lynx.isc.org/current/index.html
2. http://lynx.isc.org/current/lynx2-8-8/lynx_help/lynx_help_main.html
3. http://lynx.isc.org/current/lynx2-8-8/lynx_help/Lynx_users_guide.html
4. http://lynx.isc.org/lynx2.8.7/index.html
5. http://lynx.isc.org/lynx2.8.7/lynx2-8-7/lynx_help/lynx_help_main.html
6. http://lynx.isc.org/lynx2.8.7/lynx2-8-7/lynx_help/Lynx_users_guide.html
7. http://lynx.isc.org/mirrors.html
8. http://lists.nongnu.org/mailman/listinfo/lynx-dev/
9. http://lynx.isc.org/signatures.html
10. http://validator.w3.org/check?uri=http%3A%2F%2Flynx.isc.org%2Findex.html