Crontab not executing script and log file not working [duplicate] - bash

This question already has answers here:
Permission denied when logging on /var/log from a php script
(2 answers)
CronJob not running
(19 answers)
Closed 2 years ago.
I am struggling to create a cron job since morning. Here is the process I did.
The task I am trying to run management.sh,
#!/bin/bash
cd /var/www/mysite.com/myproject/
source ../venv/bin/activate
python manage.py dbbackup
deactivate
I made this script executable by doing sudo chmod +x /var/www/mysite.com/myproject/management.sh.
Then with crontab -e I added the following line to run every 2 mins.
*/2 * * * * /usr/bin/sh /var/www/mysite.com/myproject/management.sh >> /var/log/cron.log 2>&1
Problem
sudo tail -f /var/log/syslog | grep CRON, this is what I see,
Jul 26 09:52:01 test-svr-loc1 CRON[23583]: (dexter) CMD (/usr/bin/sh /var/www/mysite.com/myproject/management.sh >> /var/log/cron.log 2>&1)
Jul 26 09:52:01 test-svr-loc1 CRON[23582]: (CRON) info (No MTA installed, discarding output)
When I check the cron.log, it is empty.
What is wrong here?

Crontab is executing script. As per #Cyrus's suggestion in the comments, it was a permission issue. Cron was not able to write to my log as I created it with sudo. Changed permission back to me and it is now logging.

Related

/bin/sh: 1: user: not found when running shell script using crontab [duplicate]

This question already has answers here:
CronJob not running
(19 answers)
Closed 28 days ago.
Hi I'm getting this error when running the shell script using crontab for the specific user
this my crontab:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
29 22 * * * automation /home/automation/profilecheck/untitled.sh >> /home/automation/profilecheck/profilecheck.log 2>&1
anyone can help with this issue?
Cron jobs running with crontab -e are run as the user who issues that command and thus owns that cronjob. You need to remove the user automation from your command and create/run the cronjob from automation user account.
29 22 * * * /home/automation/profilecheck/untitled.sh >> /home/automation/profilecheck/profilecheck.log 2>&1
O
Or you can add your cronjob into /etc/crontab
Or move your cronjob to /etc/cron.d and make sure the user is available and has relevant permissions.

Crontab not working (standard troubleshooting done)

SHELL=/bin/bash
* * * * * /home/VI/vserver/jira_extractor/bash_scripts/cronbg.sh
I edit crontab with crontab -e as a local user.
Cron is running root 7296 1 0 17:28 ? 00:00:00
/usr/sbin/cron -f
My script runs when using exactly the same path as specified in the
crontab
There is an empty line at the end of the crontab script.
I have specified that crontab will run in bash shell
Any hints on trouble shooting will be greatly appreciated
In my case, adding the environment variables in the bash scripts to my crontab file solved the problem
Does the user allow to run cron?
Print out /etc/cron.allow & /etc/cron.deny.
Crontab troubleshooting will require to check log file /var/log/syslog. Assuming that user is allowed to run cron, then there will be an entry when your schedule job is run.
Ref: https://help.ubuntu.com/community/CronHowto

Cron and Bash script: impossible to execute

I've made a bash script that execute a docker command to dump a MySQL database:
dump_db.sh
#!/bin/bash
time=$( date +%Y%m%d%H%M )
currdir=$( pwd )
cat $currdir/container_list | while read container; do
echo "" | docker exec -i $container mysqldump -u <user> -p<password> <dbname> > $currdir/$container-$time.sql
done
If i try to run the script manually, all works fine, but if i use cron, the script does not execute.
My crontab is:
PATH=<same environment>
26 17 * * * /bin/sh /path/to/script/dump_db.sh
as you can see, i've tried also to export PATH (and checked it via env command in the crontab), but nothing.
I've also tried with these lines
26 17 * * * /bin/bash /path/to/script/dump_db.sh
26 17 * * * /path/to/script/dump_db.sh
Furthermore, also a simple bash script, like this:
#!/bin/bash
touch test_touch.txt
does not work, while the simple touch command via cron it is ok.
Where am i wrong?
Kindly check if the script has execution permission for other user.
When you're running manually you're running with the user you're logged in.
But when its running from Cron its another user. Sot that user has to have the permission to execute that script.
You may check this post for more help

How to configure crontab to run ec2-automate-backup.sh script

Hi guys am trying to automate the backup of snapshots for my ec2 volumes on Amazon. I am following the ec2-automate-backup script by Collin Johnson
If run the command on command line it is creating the snapshot (working):
ubuntu#linuxserver:/usr/local/ec2/scripts$ sudo ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
For testing purposes if i create a crontab its not working
0 10 * * * ubuntu /usr/local/ec2/scripts/ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c /usr/local/ec2/scripts/cron-primer.sh -r "eu-west-1"
Where is my problem here am running the script on ubuntu 14.04 - Amazon?
In crontab file, to execute a shell script you can use one of the following approach:
1. Call the shell script direcly, i.e.
0 10 * * * /path/to/script.sh
where the script.sh should be made executable.
2. Execute the script by sh utility, i.e.
0 10 * * * sh /path/to/script.sh
here the script.sh need not be made executable.
Now, if in your case, you need to go to a specific path and then execute script, then :
Either provide the full path of the script in crontab file directly, or
Enclose the execution commands in other shell file, and execute the enclosing file from cron.
There are two possibilites:
You need root access to run the script. You can solve this by modifying root's crontab:
sudo crontab -e
See How to run a cron job using the sudo command
You need to be in the same directory as the script to execute it
0 10 * * * ubuntu cd /usr/local/ec2/scripts && ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
See What is the 'working directory' when cron executes a job

Running a Shell Script as a Cronjob

I have a written a shell script to automate a build process.
The script checksout some code from an SVN repo, compiles and builds the code before extracting the built binary files and storing these in a central location.
I can manually execute the script ./autobuild.sh and it runs perfectly. There are a few sudo commands executed throughout the script, but I echo the password through for the first sudo command and the password holds for the entire time:
echo mypassword! | sudo -S make clean
When I add executing the script as a crontab it fails to complete all the tasks. I've tried to add it as a cronjob for the normal and root users.
Running crontab -e on my normal user account, I want the script to run at ten past midnight every day:
10 0 * * * /home/username/autobuild.sh
Also running a 32-but Cent OS 7 install with all the latest updates installed.
Can anyone provide any suggestions as to why it might work manually but not when run through a cron?
Try this
10 0 * * * /bin/bash /home/username/autobuild.sh
Step 1. find bash path
:~# whereis bash
Output
bash: /usr/bin/bash
step 2. create sh file add the line on top replace with your bash path
#!/usr/bin/bash
step 3. Make the script executable with command chmod +x .
step 4. add cron like this in for every minute to test
crontab -e
*/1 * * * * /usr/bin/bash ~/backup.sh >>test.log

Resources