I created this test bash script
#! /bin/bash
echo "hello"
mkdir "/karan/washere"
cron job i created, i want to run this cron job to run every min and want the log
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
I am signed in the droplet as root user
I also have given the permission for the script, using
sudo chmod u+x test.sh
I tried to log the syslog using
sudo grep CRON /var/log/syslog
but didn't show there also,
let me know if you need any more info or other context,
cron uses sh to interpret the commands, not bash. sh has less features and the &> is not valid in sh.
A better way for your line in the crontab would be:
1 * * * * /user/local/bin/bash /root/test.sh >> /root/crontest.log 2>> /root/crontest.err
This will append to the logging instead of overwriting, and it will separate log from errors.
If you want, however, you can force cron to use bash instead of sh. Your crontab should then be:
SHELL=/bin/bash
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
Related
I wanna run a script using crontab in every minute. The script is located at /Users/robin47/Desktop/script/demo.sh. As a side note, I give the execute permission of demo.sh file with chmod +x demo.sh command.
But it doesn't execute the script. Here is the process I'm following:
Run crontab -e from the terminal.
robin47#mac-pro ~ % crontab -e
this open a file(crontab.xxxxxxx) in nano which is located under tmp folder, as an example File: /tmp/crontab.V4DcXEJU8g.
This is my crontab.xxxxxxx file:
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /Users/robin47/Desktop/script/demo.sh
Get from here, this doesn't execute my demo.sh script.
If I run sh command from terminal like sh demo.sh, it works. And if I directly echo(or run any command like touch, mkdir) in crontab like this
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * echo 'hello' >> /Users/robin47/Desktop/script/text.txt
It also works. But doesn't run the script using crontab. I appreciate your help, Thanks.
This is my demo.sh file:
#!/bin/sh
echo "hello world" >> /Users/robin47/Desktop/script/text.txt
refer below site for cron entries , also check your script have executable permission
chmod oug+x demo.sh
https://crontab.guru/
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
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
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
where to check the logs of cron job?
I did pgrep cron it gave 2358
What does it mean?
0 02 * * * /feedbatch/application/scripts/baselineUpdate.sh
0 13 * * * /feedbatch/application/scripts/baselineUpdate.sh
#0,10,20,30,40,50 * * * * /feedbatch/application/scripts/partialUpdate.sh
*/2 * * * * /feedbatch/application/scripts/partialUpdate.sh
15 0 * * * /usr/sbin/logrotate -s $HOME/logrotate.status -f $HOME/endeca-logrotate.conf
16 0 * * * /usr/sbin/logrotate -s $HOME/outputrotate.status -f $HOME/endeca-outputrotate.conf
Your scripts are in this location -- /feedbatch/
Please check whether a directory named feedbatch is there in / . and whether a normal user has permission to run the scripts inside it if the directory exists. Try to put all your scripts in a new bin folder.. thats a good practice..
Other scripts are in /usr/sbin/....
Here also the permission may be an issue.
Try to put all these inside the root crontab. So that there wont be any permission issues.
Also check if your username is there in /etc/cron.allow and /etc/cron.deny.Dont worry If these two files are not present. But if these files are there, please make sure that your name is there in cron.allow and not there in cron.deny
to access the root crontab do the following
su - root
crontab -e
The cron daemon will log to default system logger. So it will be logged to messages or syslog.
You can configure the rsyslog to log all cron log output separate file. This is a bit depending of your linux distribution. As follow an example to write cron log to separate file for Ubuntu12.04:
edit /etc/rsyslog.d/50-default.conf (uncommand the cron.* line)
...
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
#daemon.* -/var/log/daemon.log
...
restart cron daemon.
Your cron output should be logged to /var/log/cron.log now.
That should get your an better overview about "cron doing".