crontab: which one is the right job definition? - linux

Which one is the right definition for a crontab job?
With or without the user before the execution path?
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * <user> <command>
On Debian, crontab -l show a backup example as:
....
For example, you can run a backup of all your user accounts
at 5 a.m every week with:
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
...
There is no user here!!
The /etc/crontab content (on the same box), gives a different clue:
....
and files in /etc/cron.d. These files also have username fields,
that none of the other crontabs do.
...

crontab does not * allow for specifying a user to run as...
... unless you're in the root crontab.
If you check my favorite linux admin reference, you'll not near the bottom that there are some tricks to running certain chrontab entries as a particular user. However, the best practice, if you wish to do so, would be to edit the crontab of the user:
crontab -u <username> -e
If you must...
0 0 * * * sudo -u [user] [command]
But this can only be done in the crontab of a user with sudo permissions, and as fcm pointed out, such a user could just edit the root crontab.
Most flavors of 'NIX require a user in the root crontab /etc/crontab
0 0 * * * [user] [command]
Conclusion
If you want to specify which user is running a specific cron job, the best practice is to do one of the following, depending on the use-case:
root crontab
/etc/crontab
sudo crontab
<time> <user> <command>
user crontab
crontab -u <username> -e
<time> <command>

Related

crontab doesnt work in linux but manually works

I made a shell script to connect using ssh with password to another linux server, get the details of a specific file and save it in a log file in the origin server. Manually works without problem but with the /etc/crontab doesnt update the file. (I think it may be the connection because if I try to write a test text in the file it works fine).
I tried with tee -a command and the >> command to update the file and both fails.
This is my code
#! /bin/bash
sshpass -p "password" ssh "username"#"ipserver" ls -l /filepath/file.txt | tee /home/user/test/details.log
I omitted the password, username and ip for discretion.
This is the /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
* * * * * user-name command to be executed
*/1 * * * * root /home/test/script.sh
Hope you can help me.
Thanks
My first question is, why do you run your script as root? This opens up security holes if the script itself or the directory /home/test is not owned by root.
If you are running the script as root from crontab, have you tested it as root from the command line?
I see directories /home/test and /home/user/test in your post. Do they really both exist and why?
Imho, what you should do:
Setup public key authentication between your user test and the remote user on the system. Use ssh-keygen to generate a public/private key pair, then copy the public key onto the target system and save it to ~/.ssh/authorized_keys. This will eliminate you having to use sshpass and have the password in cleartext in your crontab file.
Test the script as user test and then install it in the user test's crontab file. Simply type crontab -e as user test and you can add the same entry there.

crontab not being executed into a container after setting up permissions

I'm running a docker container with an image:
ubi8/ubi-minimal
The cronjob has correct path and go packet is already installed:
crontab -l
*/2 * * * * go run /usr/local/src/script.go
The file has correct permissions:
-rw-r-xr-x 1 root root 6329 Jun 16 15:10 script.go
However the crontab -e is like this:
/bin/sh: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127
and
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
The crontab was added in the dockerfile like this:
RUN crontab -l | { cat; echo "*/2 * * * * go run /usr/local/src/script.go"; } | crontab -
I think is correctly setup isn't it?
the crontab should execute the script every 2 minuts but it's not. Also the image is minimal and I cannot edit any file I just included some permissions to the files from the dockerfile.
If needed to change any Path from crontab I have to do this trough the dockerfile.
As it sounds like a lot of trouble, consider skipping the cron daemon entirely and just sleep in a loop
#!/bin/sh
while true; do
TIME_LOOP_START=$(date +%s) # integer time in seconds
script.go
# calculate offset for 2 minutes in seconds
sleep $(($TIME_LOOP_START + 120 - $(date +%s)))
done
adapted from
https://askubuntu.com/questions/852070/automatically-run-a-command-every-5-minutes
Get current time in seconds since the Epoch on Linux, Bash
You may find this is even better extended by making the time and target executable arguments $1 $2
You need to start the cron daemon. Here's a Dockerfile I made to illustrate
FROM registry.access.redhat.com/ubi8/ubi-minimal
RUN microdnf update && microdnf install cronie
RUN crontab -l | { cat; echo "*/2 * * * * /usr/local/src/script.sh"; } | crontab -
COPY script.sh /usr/local/src/
CMD crond -n
Note that the CMD runs crond with the -n option which keeps crond in the foreground. If we let it daemonize, docker would see that the process had ended and would terminate the container.
Instead of using go, I made a small shell script like this, called script.sh
#/bin/sh
echo Hello from script >> ~/log.txt
It writes to /root/log.txt every 2 minutes.

Why my crontab can not work with /etc/crontab file

In my project, I want to make a scheduled task with cron.
So I added a line into /etc/crontab
*/10 * * * * root /home/JobidUserJobname/JobidUserJobname.sh
and the content of /etc/crontab is like:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/10 * * * * root /home/JobidUserJobname/JobidUserJobname.sh
*/1 * * * * root date
and I restart the crontab service:
#service crond restart
#service crond reload
and they executed successfully.
But when I executed:
#crontab -l
It shows:
no crontab for root
It seems nothing wrong. My linux OS is:
CentOS release 6.5 (Final)
Who can help me?
The crontab -l gives the output "no crontab for root". This is expected since it will show only crontabs added using the command crontab -e. The crons added in "/etc/crontab" will not be listed in crontab -l command.
Things to check.
1. Check if the file "/home/JobidUserJobname/JobidUserJobname.sh" has execute permission. If no then execute the below command.
chmod +x /home/JobidUserJobname/JobidUserJobname.sh
If its still not working append a "/bin/sh" before the script.
*/10 * * * * root /bin/sh /home/JobidUserJobname/JobidUserJobname.sh
Check the cron logs to see if there are any errors.

Doesn't run this commands on mautic cron job

Hi guys i have the next problem(sorry for my english):
I want to executate a command in Mautic cron job, i put the next comands:
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:segments:update
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:campaigns:update
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:campaigns:trigger
I try a lot of things but no one of them work, like:
*/1 * * * * root /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:segments:update
or
*/1 * * * * bitnami php /apps/mautic/htdocs/app/console mautic:segments:update
But i don't know what fail's,if it's the user name,the route to php,the comand,if they doesn't have permission...
If i put this manually work perfectly
php /apps/mautic/htdocs/app/console mautic:segments:update
ty so much
You should check whether the path of the php program correct. You can check the the full path of the php program in terminal output by this command:
which php
To create a cron job, should always use the full path of the program. The crontab default environment is not like the logged in user. The program may not be found if the path is not defined to the crontab environment.
The crontab syntax composed of two parts, datetime to execute & command to be executed. User is not required to add before the command.
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
I had the same problem, it turns out that my root user wasn't enabled, therefore there wasn't any way for the system to log in with a root account to perform the cronjobs.
I just made sure that I had my root account enabled with a valid pasword.
Check that by typing:
sudo passwd --status root
If this is the case, just try to change your root password
sudo passwd root

Crontab is not working on Amazon EC2 server

Crontab is not working on Amazon EC2 Linux Server.
I have saved below codes in /etc/crontab file
crontab
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web
I have started crontab command already, but this one didn't work.
I also have saved this line in "crontab -e" too, but cron won't work.
* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web
Is there anyone who had same experience like me?
Thank you.
I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user. For example:
0 12 * * * python3 example.py
In fact, specifying a user here prevented it from running.
Solved the problem.
I used this code and it works!
* 2 * * * root tar cvfpz /home/backup/web_`date +\%Y\%m\%d`.tar.gz /home/web
You should use crontab -e to create cron for the logged user, so that you don't need to inform the username.
See here: https://stackoverflow.com/a/16986464/1777152
For people who are dealing with AWS machines and EBS you need to specify the root keyword before the command since ec2-user isn't allowed to run crontabs. Of course there's a way to fix that.
you can edit the crontab by typing sudo nano /etc/cron.d/mycrontabs or crontab -e
* * * * * root bla bla
Also make sure e that the file is ended with a new line
Don't use nano, use the native sudo crontab -e command.

Resources