This question already has an answer here:
How is % (percent sign) special in crontab?
(1 answer)
Closed 24 days ago.
I have the following user crontab entry on a RHEL 6 machine (sensitive values have been replaced):
MAILTO=cron-errors#organisation.com
0 5 * * * ~/bin/app_state.sh host-arg 9200 > ~/state/app-state-$(hostname)-$(date +%F).json
Which produces this entry in /var/log/cron:
Apr 23 05:00:08 host CROND[13901]: (dbjobs) CMD (~/bin/app_state.sh host-arg 9200 > ~/state/app-state-$(hostname)-$(date +)
But no file.
After changing the statement to:
43 5 * * * ~/bin/app_state.sh host-arg 9200 > ~/state/app-state-static.json
I get a better log entry and the file is created at ~/state/app-state-static.json
I'm sure there's some issue with not escaping the +%F but can't for the life of me find details of how I should be escaping it. I could wrap the filename generation inside another shell script but this is more easy to read for people coming looking for the file.
As crontab tag wiki says, percent characters are problematic in crontabs; % gets converted to a newline:
[..] > ~/state/app-state-$(hostname)-$(date +%F).json
would run command as
[..] > ~/state/app-state-$(hostname)-$(date +\nF).json
“Escaping” percent characters is possible, but the escape character gets executed in the command. A job like following would run the command with \ in front of percent character.
0 5 * * * ~/bin/app_state.sh host-arg 9200 > ~/state/app-state-$(hostname)-$(date +%F).json
One way to circumvent this problem is to have the command in a script and execute that as a cron job:
/usr/local/bin/app_state_cron.sh:
#!/bin/sh
~/bin/app_state.sh host-arg 9200 > ~/state/app-state-$(hostname)-$(date +%F).json
And in crontab:
0 5 * * * /bin/sh /usr/local/bin/app_state_cron.sh
Related
This question already has answers here:
CronJob not running
(19 answers)
Closed 1 year ago.
i have problem with crontable in shell
I always write cronjob like this
0 * * * * /tmp/myscript
but now im on different shell and to use my script i need write like this
bash-4.2$ bash /tmp/myscript
And thats problem because when I write cronjob like this
* * * * * /tmp/myscript or bash /tmp/myscript ---> thats not work
How to solve this problem?
You must use this structure for Crontab:
* * * * * user-name command to be executed
By the way, first, you should rename myscript to myscript.sh and run it.
For example:
17 * * * * root bash /tmp/myscript.sh
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.
I would like to know how can I run an application with crontab if the app uses flags:
30 0 * * * root myapp --run
I have searched similar questions here but I haven't found any approximate issue.
You are able to use as much params as you want, everything after 'schedule expression' treated as your execution command
30 0 * * * /sbin/ping -c 1 192.168.0.1 > /dev/null
This question already has answers here:
How to write a cron that will run a script every day at midnight?
(6 answers)
Closed 7 years ago.
I have a simple shell script that just checks the contents of a directory and if anything was added during the day makes a copy of it to a backup folder.
I'd like to execute this script at the end of each day (let's assume at 23:55).
The system(Debian) which this scripts reside on it, is always on (kind of server)
How can I do that?
To add a crontab job, type the following command at a UNIX/Linux shell prompt:
$ sudo crontab -e
Add the following line:
1 2 3 4 5 /path/to/script
where
1: Minutes (0-59)
2: Hours (0-23)
3: Days (1-31)
4: Month (1-12)
5: Day of the week(1-7)
/path/to/script - your own shell script
In your case it would be:
55 23 * * * /path/to/yourShellScript
You want to edit your crontab file using
crontab -e
Then you want to add
55 23 * * * COMMAND TO BE EXECUTED
for more info look at this
I'm anything, but a linux expert, but a quick Google search conjured up this:
watch -n <your time> <your command/script>
This should do the trick. For more information, check this out: http://www.linfo.org/watch.html
sudo crontab -e
55 23 * * * some_shell_script.sh
Check out the Cron task scheduler built in to Debian. Simply add an entry for your script to your crontab file (see: https://help.ubuntu.com/community/CronHowto).
This have been discussed several times in previous post. I followed given advise but it does not work for me. I have two scripts which are run by the cron service every minute. To my surprise, only one runs per minute( 1st in the list below), the other fails (2nd in list below). Most surprising, when run direct from the terminal, both scripts execute fine.
Cron setup :
*/1 * * * * /home/user/Desktop/scripts/generatepattern.sh
*/1 * * * * /home/user/Desktop/scripts/getnextfile.sh
File permissions are:
-rwxr--r-- 1 user user 522 Jul 25 16:18 generatepattern.sh
-rwxr--r-- 1 user user 312 Jul 25 23:02 getnextfile.sh
The code for the non-schedulable( not running in cron ) is :
#!/bin/bash
#Generate a file to be used for the search
cd /home/user/Desktop/scripts
no=`cat filecount.txt`
if test $no -lt 20
then
#echo "echo less"
#echo $no
expr `cat filecount.txt` + 1 >filecount.txt
fi
In the last line you wrote cat filecount.txt instead of cat /home/user/Desktop/scripts/filecount.txt
I discovered the main issue was that the new cron settings only get used when the vi editor gets closed. Changes have to be made on the editor and a :wq command issued so that the new settings get installed. Just issuing :w command does not work since no install happens(this was my mistake). I realised this after issuing :wq command on vi and the following output displayed :-
# crontab -e
crontab: installing new crontab
Thanks to all other suggestions made.