Cron job is not running on Linux - linux

At first, this is not exactly a programming question and more specific to Linux. I hope that it can be answered here though.
I have created a cron job which will execute a shell script every 2 minutes on my machine. However, the cronjob is not executing.
output of crontab -e command
2 * * * * /home/yuri/connector.sh >> /home/yuri/test.txt 2>&1
I have the cron daemon running:
ps aux | grep cron
root 944 0.0 0.0 19120 932 ? Ss 08:25 0:02 cron
1000 19619 0.0 0.0 13600 880 pts/2 S+ 21:50 0:00 grep --color=auto cron
The connector.sh shell script runs properly when I execute it manually, however when run through the cron job created above, it does not work.
I have redirected the output to a text file to know if something is going wrong while executing the cron job, but no such text file is created.

Your cron job isn't set to run every two minutes, it's set to run on the second minute of every hour. You might change it as follows:
*/2 * * * * /home/yuri/connector.sh >> /home/yuri/test.txt 2>&1

Running every 2 minutes should be given as
*/2 * * * * /home/yuri/connector.sh >> /home/yuri/test.txt 2>&1.
Does this work ?

Related

Cron won't execute none of my commands on Ubuntu 21.10 impish

I'm trying to run a Docker container every other minute that is stopped via cron job but it seems not working.
What I've done is launch the command crontab -e and add the line
*/1 * * * * docker start sender >> /home/cronlog.log 2>&1
I've added the user group to Docker as explained here (in fact I can access docker from the terminal without sudo)
I have also tried to add the command into a script as below
*/1 * * * * /home/start_container.sh >> /home/cronlog.log 2>&1
with the script containing
#!/bin/sh
docker start sender
but still, nothing happens. The cron process is working tho as using the command ps -ef | grep cron I got
root 881 1 0 08:42 ? 00:00:00 /usr/sbin/cron -f -P
nicola 10905 10178 0 11:31 pts/0 00:00:00 grep --color=auto cron
Am I missing something? (Obviously, the commands work if launched manually from the terminal)
Try using the docker path instead.
type the following command to get the path of docker.
$ where docker
/usr/bin/docker
/bin/docker
then try any one of the paths in the cron script
*/1 * * * * /bin/docker start sender >> /home/cronlog.log 2>&1
or
*/1 * * * * /usr/bin/docker start sender >> /home/cronlog.log 2>&1
It turned out that, for some reason, the cron doesn't like the /home/ (at least, in this specific instance)
I've fixed using another path such as
*/1 * * * * docker start sender >> /tmp/cronlog.log 2>&1

Created cron job to run every 2 mint

I have configured cron job but it's not working.
I wanted to run the myfile.sh script for every 2 mint and below are my configuration in crontab.
# m h dom mon dow comman
2 * * * * /home/ubuntu/myfile.sh
myfile.sh is executable and contains below lines of code
#!/bin/bash
mysqldump -u[user] -p[password] --single-transaction --routines --triggers --all-databases > /home/ubuntu/backup_db10.sql
Is there anywhere we need to add configure anything?
You're running the script at two minutes past every hour. As in 1:02, 2:02 and so on.
You can change it to something like
*/2 * * * * /home/ubuntu/myfile.sh
to run it every two minutes.
A bit more info can be found here.

Crontab command not working

I know this question has been asked many, many times and I've done a lot of research but still I'm not able to run this extremely simple cron:
$ crontab -l
* * * * * /bin/date
This should, ideally, print the date every minute.
There is no cron.allow or cron.deny file, and the cron daemon is working:
ps -e | grep cron
1119 ? 00:00:00 cron
17646 ? 00:00:00 cron
Any idea what might be wrong?
Cron processes run in a separate subprocess of their own, so the output of a cron job will not be visible to you in your shell.
Instead, you will have to capture the output of your cron commands and save them. So, set your cronjob like:
* * * * * /bin/date >> /home/user/date.log
And now, if you will tail this log file, you will start seeing the result.

Cron starts same process twice

I have crontab configured like this:
*/2 * * * * php /home/ec2-user/myapp/myscript.php >> /home/ec2-user/myapp/log/myapp.log 2>&1
When I execute ps aux, I see the following output:
ec2-user 1296 0.0 0.0 2984 992 ? Ss 15:36 0:00 /bin/sh -c /home/ec2-user/myapp/myscript.php >> /home/ec2-user/myapp/log/myapp.log 2>&1 SHELL=/bin/sh HOME=/home/ec2-user PATH=/usr/bin:/bin LOGNAME=ec2-user USER=ec2-user
ec2-user 1299 0.3 3.7 91528 63612 ? S 15:36 0:16 /home/ec2-user/myapp/myscript.php SHELL=/bin/sh USER=ec2-user PATH=/usr/bin:/bin PWD=/home/ec2-user SHLVL=1 HOME=/home/ec2-user LOGNAME=ec2-user _=/usr/bin/php
To me it looks like same process was started twice at the same time, process one with PID 1296, process two with PID 1299.
Is that normal? Why two processes are in ps output instead of one?
It looks like /bin/sh is used to invoke your cron tabs, so that they run in their own environment, that's the first process. Then /bin/sh invokes php to run your actual script, and php is your second process. There's nothing wrong with this.
Try to add exec before php
*/2 * * * * exec /usr/bin/php /home/ec2-user/myapp/myscript.php >> /home/ec2-user/myapp/log/myapp.log 2>&1
The shell process /bin/sh will be replaced by exec

Scheduling shell script in crontab

I have a shell script that I can run with root user this way:
root#vivid-15:~# ./backup.sh
It's on /root/backup.sh. Now, how do I schedule it on crontab to execute every day at 01:00 AM? I done this:
0 1 * * * root
But now I don't know how to proceed the command to do that.
Have you tried this? Also, a "1" in the hour field means 1am, not 1pm.
0 1 * * * root /root/backup.sh
Edit: changed the 13 (1pm) back to 1 (1am).
Crontab format:
MIN HOUR DAY MON WEEKDAY CMD
I don't know that you need to define what user you want it to run as when its in crontab -- commands will be run as the user who makes the entries with crontab -e. To create a cron process that runs as root, either login as root or set it up with $ sudo crontab -e
I think you're looking for something more like this:
0 1 * * * /root/backup.sh

Resources