Cannot make CRON work for user on CentOS 7 - cron

This is file /var/spool/cron/[USER]:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
0 */2 * * * /usr/bin/wget -O - http://[WEBSITE]/[STUFF] >/dev/null 2>&1
I made this CRON to visit website every second hour. But it is not working, because when I type command about CRON status, then it says this:
$ systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded ...
Active: active (running) ...
...
crond[6967]: (CRON) bad minute (/var/spool/cron/[USER])
...
It is weird, because when I move it to the /var/spool/cron/root, then it works. This "bad minute" shows only for other users. But why?
CHMOD is 600 and owner is the user I talk about.

I think you shouldn't edit the cron file directly, have you tried editing it when logged in to the correct user with this command:
crontab -e
Also check that your /etc/crontab file is correct
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

Related

Running aide --check as a crontab job once a week

I'm having real problems getting a simple (aide --check) job to run as a crontab job. I've tried everything I can think of but it won't seem to run. I've tried specifying the shell in crontab:
SHELL=/bin/bash
I've tried all kinds of variations of the command line:
*/1 * * * * root /bin/bash /usr/sbin/aide --check
*/1 * * * * /bin/bash /usr/sbin/aide --check
*/1 * * * * root /usr/sbin/aide --check
*/1 * * * * root /bin/bash /usr/sbin/aide --check >> /var/log/SystemFileCheck.log
Plus others but just can't get it to run. I've followed online guides which all say I'm doing it correctly. I've tried putting it into a bash script instead and running that but no luck. What am I doing wrong?
These are some of the errors I'm getting:
Mar 30 11:25:01 localhost CROND[14060]: (root) CMD (root /bin/bash /usr/sbin/aide --check >> /var/log/SystemFileCheck.log)
Mar 30 11:25:01 localhost CROND[14058]: (root) CMDOUT (/bin/sh: root: command not found)
Mar 30 11:28:01 localhost CROND[14397]: (root) CMD (root /bin/SystemIntegCheck.sh >> /var/log/SystemFileCheck.log)
Mar 30 11:28:01 localhost CROND[14395]: (root) CMDOUT (/bin/bash: root: command not found)
Mar 30 11:39:01 localhost CROND[16094]: (root) CMD (/bin/bash /usr/sbin/aide --check)
Mar 30 11:39:01 localhost CROND[16092]: (root) CMDOUT (/usr/sbin/aide: /usr/sbin/aide: cannot execute binary file)
Can anyone shed some light on this?
Thanks in advance
PS. the once a minute is just for testing
A user id can only be specified in the system crontab file. The entries of a user's crontab file don't take a user id. The entries in question are apparently found in a user's crontab file, which is why you get root: command not found from the first, third and fourth entries.
From the second, you get cannot execute binary file because you ask bash to execute /usr/sbin/aide as a bash script when it's not a bash script. You should be using
*/1 * * * * /usr/sbin/aide --check

Cron jobs not running in VestaCP (CentOs)

I have written some PHP scripts that I am trying to run with cron jobs in VestaCP, but they don't seem to be running. I have tried to search for other threads on here and on the VestaCP forum that could help me identify the error, but have not found a solution.
Server system:
CentOs 7.4
Vesta 0.9.8-22
I have tested the PHP scripts by going to the links directly. They all work well. But the cron jobs are not running and I can't figure out why.
I have not been able to locate any error messages or logs generated by these cron jobs (even when I remove the "> /dev/null 2>&1"). But I might have been looking in the wrong places.
All of the cron jobs have been added through the VestaCP cron interface.
I have disabled exim, dovecot, clamd, and spamassassin. And I have turned off notifications in the cron panel. Not sure if that is related.
Copied from /var/spool/cron/admin [edited domain name]:
15 02 * * * sudo /usr/local/vesta/bin/v-update-sys-queue disk
10 00 * * * sudo /usr/local/vesta/bin/v-update-sys-queue traffic
30 03 * * * sudo /usr/local/vesta/bin/v-update-sys-queue webstats
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-queue backup
10 05 * * * sudo /usr/local/vesta/bin/v-backup-users
20 00 * * * sudo /usr/local/vesta/bin/v-update-user-stats
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-rrd
15 6 * * * sudo /usr/local/vesta/bin/v-update-sys-vesta-all
01 4 * * * sudo /usr/local/vesta/bin/v-update-letsencrypt-ssl
*/5 * * * * wget -q -O - "https://testing.example.com/cli/new-projects" > /dev/null 2>&1
*/5 * * * * wget -q -O - "https://example.com/cli/new-projects" > /dev/null 2>&1
30 10 * * * wget -q -O - "https://example.com/cli/project-expiration" > /dev/null 2>&1
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-queue letsencrypt
0 10 * * * wget -q -O - "https://testing.example.com/cli/project-expiration" > /dev/null 2>&1
*/2 * * * * wget -q -O - "https://testing.example.com/cli/email-sender" > /dev/null 2>&1
*/2 * * * * wget -q -O - "https://example.com/cli/email-sender" > /dev/null 2>&1
I had the same problem in Ubuntu.
The problem was that the cron jobs created from VestaCP control panel is created for the user and although there is sudo at the beginning of the command, they are not run.
They seem to be called since I can see them on the /tmp/log/syslog file. It does not show any error though. But for some reason, the commands are not executed.
Here are few simple commands to check cron status.
Check if Cron service is running:
pgrep cron
if a number is returned the service is running else not
Check Cron status:
systemctl status cron
Check the current cron file:
crontab -l
Edit cronjob file:
crontab -e
One solution is to create the cron jobs for the root user from the terminal. I have not tried for other users. The cron jobs created for root user will run without a problem.

crontab: task not running in my crontab entry

I have problem with crontab because my crontab entry not running
$ date
Tue Jan 6 07:21:11
$ crontab -l
*/2 6-11 * * 1-3 hostnamectl >> /home/bob/hostsname.txt
this crontab should be running every 2 minutes from 6-11 AM every monday - weds, but nothing happened. record in /var/log/cron for this cronjobs is empty
i debug to ensure crond is running and change to below
* * * * * hostnamectl >> /home/bob/hostsname_debug.txt
cronjobs running as expected every 1 minutes. i see hostsname_debug.txt in home directory.
i see the log in /var/log/cron
why the first cronjobs not running?

Cron Job in Fedora 8 - Not getting the desired result

OS : Fedora release 8
user : root
crontab -e :output
11 * * * * root /bin/touch /home/arun/Desktop/f1
But this cron job is not creating new file 'f1'.
/var/log/cron :says
Feb 21 09:11:01 localhost CROND[5762]: (root) CMD (root /bin/touch /home/arun/Desktop/f1 )
In command line I executed below command and it creates file f1.
#/bin/touch /home/arun/Desktop/f1
You are trying to execute the command root /bin/touch /home/arun/Desktop/f1
The executable 'root' obviously does not exist. Try removing 'root' (via crontab -e) and make the crontab entry look like:
11 * * * * /bin/touch /home/arun/Desktop/f1

How do I make cron run something every "N"th minute, where n % 5 == 1?

I know that I can have something run every five minutes in cron with a line like:
*/5 * * * * /my/script
What if I don't want it running at 12:00, 12:05, 12:10, but rather at 12:01, 12:06, 12:11, etc? I guess I can do this:
1,6,11,16,21,26,31,36,41,46,51,56 * * * * /my/script
...but that's ugly. Is there a more elegant way to do it?
1-56/5 * * * * /my/script
This should work on vixiecron, I'm not sure about other implementations.
Use your first schedule:
*/5 * * * * /my/script
And add this to the start of your script:
sleep 60
(Yes, this is a joke)
This is quite an old topic, however as so much time has passed there are a few other options now. One of which is not to use cron at all, and use systemd timers. Using these gives you a higher granularity than seconds along with lots of other options
More information is available here https://wiki.archlinux.org/index.php/Systemd/Timers
eg to run a adhoc command
# systemd-run --on-calendar="*:1/5" /bin/touch /tmp/foo2
Running timer as unit run-r31335c4878f24f90b02c8ebed319ca60.timer.
Will run service as unit run-r31335c4878f24f90b02c8ebed319ca60.service.
# systemctl status run-r31335c4878f24f90b02c8ebed319ca60.timer
● run-r31335c4878f24f90b02c8ebed319ca60.timer - /bin/touch /tmp/foo2
Loaded: loaded
Transient: yes
Drop-In: /run/systemd/system/run-r31335c4878f24f90b02c8ebed319ca60.timer.d
└─50-Description.conf, 50-OnCalendar.conf, 50-RemainAfterElapse.conf
Active: active (waiting) since Wed 2017-10-25 09:05:13 UTC; 40s ago
# ls -l /tmp/foo*
-rw-r--r-- 1 root root 0 Oct 25 09:06 /tmp/foo2
# sleep 300; ls -l /tmp/foo*
-rw-r--r-- 1 root root 0 Oct 25 09:11 /tmp/foo2
# date; ls -l /tmp/foo2
Wed Oct 25 09:21:42 UTC 2017
-rw-r--r-- 1 root root 0 Oct 25 09:21 /tmp/foo2
edit: these type of timers wont persist over reboot, if you want them to make sure you generate a proper service file, with the relevant oncalendar line
I'd create a new script "delaystart" that takes a sleeping period as first parameter and the script to run as the rest. I'd make the script check the crontab line for the line with the script and only start the script if the line is not commented out. That makes it reusable, and ps won't report the script as running when it really isn't.
#!/bin/bash
sleeptime=$1
sleep ${sleeptime}
shift
if ( ! crontab -l | grep -e '#.+delaystart '${sleeptime} $* ) then
$*
fi
sean.bright's joke got me thinking... why not use...
* * * * * /my/script
...and within the script do this...
#!/bin/bash
export WHEN=`date '+%M'`
echo $WHEN
export DOIT=`echo "$WHEN % 5" | bc`
echo $DOIT
if [ $DOIT != 0 ] ; then
echo "ha ha ha"
fi
echo "done"
...a kludge... maybe, but as ugly as the crontab... I don't know.

Resources