Cron script executed but no output - linux

I've taken a look to many different topics and did not find an answer to my problem. I created 2 bash scripts on my Ubuntu server and I'm trying to execute them periodically. It seems they are running, but they produce nothing. They are executable:
drwxr-xr-x 14 root root 4096 Mar 14 18:02 ..
-rwxr-xr-x 1 root root 2623 Apr 16 21:18 backup.pl
-rw-r--r-- 1 root root 87066352 May 10 21:37 full_site_backup-10-4-2018.tar.gz
-rwxr-xr-x 1 root root 530 May 11 20:21 checkHealth.sh
drwxr-xr-x 2 root root 4096 May 11 20:35 .
So here is one of my scripts:
#!/bin/bash
# log stdout and stderr to two different files
exec >>/var/log/test.log 2>>/var/log/test.err.log
# ...and log every command we try to execute to stderr (aka looog.err.log)
# set -x
CODE=$(curl -s -o /dev/null -I -A "myuseragent" -w "%{http_code}" https://www.xxxxxxxxxxxxx.xxx/xxxx)
DATE=$(date)
if [ $CODE -gt 300 ]
then
service mysql restart
service tomcat8 restart
>&2 echo "$DATE - KO !!!!!! code retour $CODE"
else
echo "$DATE - OK, code $CODE"
fi
and here is my sudo crontab -e :
# m h dom mon dow command
0 2 * * * root /usr/bin/perl /var/backup/backup.pl
* * * * * root /bin/sh /var/backup/checkHealth.sh
and here is my sudo tail -f /var/log/cron.log:
May 11 20:39:01 ns381471 CRON[10778]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
May 11 20:39:26 ns381471 crontab[10823]: (root) BEGIN EDIT (root)
May 11 20:40:01 ns381471 CRON[10880]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
May 11 20:40:01 ns381471 CRON[10879]: (root) CMD (/usr/local/rtm/bin/rtm 2 > /dev/null 2> /dev/null)
May 11 20:40:30 ns381471 crontab[10823]: (root) END EDIT (root)
May 11 20:41:01 ns381471 CRON[10974]: (root) CMD (/usr/local/rtm/bin/rtm 2 > /dev/null 2> /dev/null)
May 11 20:41:01 ns381471 CRON[10975]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
May 11 20:42:01 ns381471 CRON[11070]: (root) CMD (/usr/local/rtm/bin/rtm 2 > /dev/null 2> /dev/null)
May 11 20:42:01 ns381471 CRON[11071]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
Any help would be appreciated. Thanks

Ok for those who are in the same situation, my crontab was
# m h dom mon dow command
0 2 * * * root /usr/bin/perl /var/backup/backup.pl
* * * * * root /bin/sh /var/backup/checkHealth.sh
It appears that I just copied the syntax you can find on many cron tutorials about editing /etc/crontab, but in my case I did a sudo crontab -e, so I did not have to specifiy the user (root), so the working entries were :
# m h dom mon dow command
0 2 * * * /usr/bin/perl /var/backup/backup.pl
* * * * * /bin/sh /var/backup/checkHealth.sh
or simplier :
# m h dom mon dow command
0 2 * * * /var/backup/backup.pl
* * * * * /var/backup/checkHealth.sh

Related

change letsencrypt certbot run time

I have setup certbot for letsencrypt key renewal. This works.
When i check my logs at /etc/log/letsencrypt i see it runs twice a day.
Somewhere around midnight and somewhere around noon.
I would like to run it only once a day somewhere in the early morning.
To get control over the running of certbot i removed all entries from my cronjobs. But unexpectedly it still runs.
So how is certbot started?
Extra info:
when i run a script to show all cronjobs i get this:
(script: How do I list all cron jobs for all users? )
mi h d m w user command
0 3 * * 1 root /usr/bin/rsnapshot weekly
0 */4 * * * root /usr/bin/rsnapshot hourly
16 10 * * * root test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond
25 6 * * * root /etc/cron.daily/apache2
25 6 * * * root /etc/cron.daily/apt-compat
25 6 * * * root /etc/cron.daily/aptitude
25 6 * * * root /etc/cron.daily/automysqlbackup
25 6 * * * root /etc/cron.daily/bsdmainutils
25 6 * * * root /etc/cron.daily/dpkg
25 6 * * * root /etc/cron.daily/exim4-base
25 6 * * * root /etc/cron.daily/logrotate
25 6 * * * root /etc/cron.daily/man-db
25 6 * * * root /etc/cron.daily/mlocate
25 6 * * * root /etc/cron.daily/ntp
25 6 * * * root /etc/cron.daily/passwd
25 6 * * * root /etc/cron.daily/popularity-contest
25 6 * * * root /etc/cron.daily/tomcat8
30 2 1 * * root /usr/bin/rsnapshot monthly
30 3 * * * root /usr/bin/rsnapshot daily
47 6 * * 7 root /etc/cron.weekly/man-db
the file from certbot i found was certbot in /etc/cron.d
it contained this line:
#0 4 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew
which i commented out (with the #).
my system is debian:
Debian GNU/Linux 9
I use certbot with Ubuntu 16.04 + nginx
There are two places to check jobs related to Certbot
1) /etc/cron.d/certbot
here you have
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew && nginx -s reload
2) systemd timer
run commnad:
$sudo systemctl list-timers
and see output
Sun 2018-07-08 00:46:59 EEST 7h left Sat 2018-07-07 12:36:26 EEST 4h 51min ago certbot.timer certbot.service
then
3)
go to check file that control time when systemctl run Certbot
/etc/systemd/system/timers.target.wants/certbot.timer
that is symbolic link for
/lib/systemd/system/certbot.timer
Pay attention to this lines that define times + random seconds (set a random time for purpose to don't stress simultaneously letsencrypt servers)
OnCalendar=--* 00,12:00:00
RandomizedDelaySec=3600

Unable to run cron job with standard user account

The same job can be executed as root, but it can't execute as a standard user.
Is it permission problem or I need to change anything, I have no idea on it.
Thanks
SunOS 5.10 Generic_150400-30 sun4v sparc SUNW,SPARC-Enterprise-T5120
Command:
1) login as a root
2) crontab -l
* * * * * /usr/bin/date > /tmp/root.log
3) /tmp/root.log is here
1) login as a Non-root user
2) crontab -l
* * * * * /usr/bin/date > /tmp/non-root.log
3) /tmp/non-root.log is not here
The following permissions are OK for the binary file date
-bash-3.2# ls -l /usr/bin/date
-r-xr-xr-x 1 root bin 11056 Jan 22 2005 /usr/bin/date
-bash-3.2#
If the permissions are OK check your cron log on /var/cron/log file
-bash-3.2# tail /var/cron/log
< root 24592 c Fri Oct 20 18:50:21 2017
> CMD: /usr/bin/date > /tmp/non-root.log
> user 25192 c Fri Oct 20 18:51:00 2017
< user 25192 c Fri Oct 20 18:51:00 2017
> CMD: /scripts/collectdata.sh > /dev/null 2>&1
> root 25769 c Fri Oct 20 18:52:00 2017
< root 25769 c Fri Oct 20 18:52:00 2017
> CMD: /scripts/collectdata.sh > /dev/null 2>&1
> root 26853 c Fri Oct 20 18:54:00 2017
< root 26853 c Fri Oct 20 18:54:00 2017
-bash-3.2#
Thanks all, I finally found out the issue.
The reason is that non-root account is locked out, I think it maybe someone did many failure attempt which make this locked.
After I passwd -u "Account", the job can be run as expected. Thanks~

Crontab throwing error

I have the following content in crontab:
20 1,7,13,18 * * * /usr/sbin/automysqlbackup
15 * * * * root find /opt/activeMq/activemq-data/localhost/KahaDB/ -mtime +10 -type f -delete
but I get this when I restart cron service:
Feb 20 08:43:27 . crontab[14584]: (root) BEGIN EDIT (root)
Feb 20 08:44:08 . crontab[14584]: (root) REPLACE (root)
Feb 20 08:44:08 . crontab[14584]: (root) END EDIT (root)
Feb 20 08:44:28 . kernel: [325740.483115] init: cron main process (14563) killed by TERM signal
Feb 20 08:44:28 . cron[14641]: (CRON) INFO (pidfile fd = 3)
Feb 20 08:44:28 . cron[14642]: (CRON) STARTUP (fork ok)
Feb 20 08:44:28 . cron[14642]: Error: bad username; while reading /etc/crontab
Feb 20 08:44:28 . cron[14642]: (*system*) ERROR (Syntax error, this crontab file will be ignored)
Feb 20 08:44:28 . cron[14642]: (CRON) INFO (Skipping #reboot jobs -- not system startup)
I found the solution. Username field is only available at system level cron(etc/crontab) and not user level cron.

Cronjob is working, but my Script wont load in cron

My cron is working(Cron.log below) and my script is working too. But Cron wont load the script every 2 Minutes.
Cron.log:
Apr 3 01:02:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[11777]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:04:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[1754]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:06:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[15086]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:06:49 Debian-78-wheezy-64-minimal crontab[5657]: (root) BEGIN EDIT (root)
Apr 3 01:06:54 Debian-78-wheezy-64-minimal crontab[5657]: (root) END EDIT (root)
Apr 3 01:06:59 Debian-78-wheezy-64-minimal crontab[19549]: (mc) BEGIN EDIT (mc)
Apr 3 01:08:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[9566]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:08:21 Debian-78-wheezy-64-minimal crontab[19549]: (mc) END EDIT (mc)
Apr 3 01:08:26 Debian-78-wheezy-64-minimal crontab[12341]: (mc) BEGIN EDIT (mc)
Apr 3 01:09:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[7312]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime))
Apr 3 01:10:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[29224]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:10:45 Debian-78-wheezy-64-minimal crontab[12341]: (mc) END EDIT (mc)
Apr 3 01:12:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[4285]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:14:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[12549]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:16:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[2767]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:17:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[18323]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
and here is my Script:
#!/bin/bash
SERVER=`ps ax | grep FTBServer-1.6.4-965.jar | grep -v grep | wc -l`
if [ $SERVER -eq 1 ]
then
exit
else
cd /home/mctw2/ ; screen -a -m -d -s mctw2 ./ServerStart.sh
fi
and my crontab at least:
*/2 * * * * mc /home/mctw2/crash-script
How can I make it work?
Your crontab entry
*/2 * * * * mc /home/mctw2/crash-script
contains the username mc in the 6th column as appropriate for system-level crontabs as in /etc/crontab.
However the format for per-user crontabs as edited using crontab -e is the following:
*/2 * * * * /home/mctw2/crash-script

Cron generated by ISPConfig

I have a problem with crontab generated by ISPConfig.
MAILTO=''
* * * * * web9 /usr/bin/wget -q -O /dev/null 'http://inz.isedo.pl/test/cron.php' >/dev/null 2>&1 #inz.isedo.pl
In log, I have a errors:
Feb 16 21:11:01 s /usr/sbin/cron[21697]: (*system*ispc_web9) RELOAD (/etc/cron.d/ispc_web9)
Feb 16 21:11:01 s /USR/SBIN/CRON[23817]: (web9) CMD (/usr/bin/wget -q -O /dev/null 'http://inz.isedo.pl/test/cron.php' >/dev/null 2>&1^I#inz.isedo.pl)
Feb 16 21:11:01 s /USR/SBIN/CRON[23816]: (CRON) error (grandchild #23817 failed with exit status 1)
Does it work from the command line ?
Can you redirect the stdout and stderr to a file (as opposed to null) and pass on the output ?

Resources