The following does work as expected:
date +'%d-%b-%Y-%H-%M'
28-Sep-2009-14-28
But none of the following 4 entries from crontab are working.
* * * * * date +\'%d-%b-%Y-%H-%M\' >> /backup/shantanu/testing.txt
* * * * * date +'%d-%b-%Y-%H-%M' >> /backup/shantanu/testing1.txt
* * * * * date +"%d-%b-%Y-%H-%M" >> /backup/shantanu/testing2.txt
* * * * * date +\"%d-%b-%Y-%H-%M\" >> /backup/shantanu/testing3.txt
Error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I can save the same code in a shell script and set the cron, but I will like to know if it is possible to directly set a cron for the task.
The actual cron entry that I am trying to set looks something like this...
16 * * * * mysqldump myDB myTB > /backup/ABCbc$(date +'%d-%b-%Y-%H-%M').sql 2> /backup/ABCbc_errORS$(date +'%d-%b-%Y-%H-%M').txt
There are four common causes for cron job commands to behave differently compared to commands typed directly into an interactive shell:
Cron provides a limited environment, e.g., a minimal $PATH, and other expected variables missing.
Cron invokes /bin/sh by default, whereas you may be using some other shell interactively.
Cron treats the % character specially (it is turned into a newline in the command).
The command may behave differently because it doesn't have a terminal available.
You must precede all % characters with a \ in a crontab file, which tells cron to just put a % in the command, e.g.
16 * * * * mysqldump myDB myTB > "/backup/ABCbc$(date +'\%d-\%b-\%Y-\%H-\%M').sql" 2> "/backup/ABCbc_errORS$(date +'\%d-\%b-\%Y-\%H-\%M').txt"
(As a separate matter, always put double quotes around a "$variable_substitution" or a "$(command substitution)", unless you know why not do it in a particular case. Otherwise, if the variable contents or command output contains whitespace or ?*\[, they will be interpreted by the shell.)
As long as there are no spaces in the format string supplied as an argument to date, you should not need the ticks at all.
date +%d-%b-%Y-%H-%M
should work.
You're using a syntax not supported by /bin/sh. Try invoking your preferred shell and passing the command as an argument.
Related
Why do .sh files have an * next to them?
It means that the file is executable.
Reference: General Output Formatting
The * means that the file is executable
I execute a sh file using command ./abc.sh abc_config
How do we get this done via cron ? for a single command i usually do
5 0 * * * /test/abc.sh
But for ./abc.sh abc_config I am a little confused
One option could be to store the script and it's argument in another script and run that instead?
5 0 * * * /test/run_abc.sh
Contents of run_abc.sh
./abc.sh abc_config
You should possibly include paths and a "shebang" matching whatever shell you want to use, ie:
#!/usr/bin/env bash
/path-to-dir-where-program-is/abc.sh abc_config
I'm trying to create a simple crontab that creates a file called log.txt every minute by populating it with a simple command's output. Right now this is what I've put into my crontab:
* * * * * (/usr/bin/ls <pathToRandomDirectory) > log.txt
By my understanding, the 5 asterisks correspond to "every minute". But when I run this the log.txt file is not being created. Is there something I'm missing here?
ALSO, if I didn't want to have an email sent to me whenever the job is created I found that I need to put the line:
>/dev/null 2>&1
Somewhere in my crontab file. Where exactly does this go? At the end of the command or on a separate line?
You have to put an absolute path for log.txt. Otherwise, it will be created in /.
Also, >/dev/null 2>&1 has to be at the end of the sentence. If you want the 2 (meaning the errors) to be dismissed, just write 2>/dev/null.
Then, your final cronjob would be like this:
* * * * * /usr/bin/ls pathToRandomDirectory > /pathToRandomDirectory/log.txt 2>/dev/null
What exactly should be in that file? This creates a new file with the text "something" in it every minute:
* * * * * echo "something" > /path/to/your/file.txt
I had some problems with crontab especially when I created a new crontab config - my problem was that I had to insert an empty line at the end of the crontab - some systems need it to work correctly. So if you do
$ crontab -l
the output should be a list of your cron jobs on each line and then an empty line at the end.
Try it if it works for you. Sorry, but I'm not sure about the email, but I dont think that you need to put lines like that to crontab, play around with the configuration - it shouldn't send you any emails by default.
In the commandline I am able to do
cp htlog.out test.$(date '+%m%d%Y')
but when I try to crontab it like this:
37 17 * * 1-5 cd the/dir && cp htlog.out test.$(date "+%m%d%Y")
It fails with the following message from the CRON DAEMON:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
And I do have endlines after the command. What is wrong with that crontab entry?
% in crontab have a special meaning, it should be backslashed, so:
37 17 * * 1-5 cd the/dir && cp htlog.out test.$(date "+\%m\%d\%Y")
From man 5 crontab :
A "%" character in the command, unless escaped with a backslash
* (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
Excuse me if it is a repeat. I have crontab entries which look like:
* * * * * sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh
How to insert a # on the line which contains a call to "as.sh" using sed?
How to uncomment it back?
You can use:
sed '/\/as.sh/s/^/#/'
which will replace the start-line zero-width marker with a comment character for all lines containing /as.sh, as shown in the following example:
pax> echo '
* * * * * sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh
' | sed '/\/as.sh/s/^/#/'
* * * * * sleep 15;/etc/opt/wer.sh
#1 * * * * /opt/sfm/qwe/as.sh
But you need to keep in mind a couple of things.
It's usually not enough to just change the file, you also need to notify cron that it needs to re-read it. This is automatic if you use the crontab command itself but you may have to send a signal to cron if you're editing the file directly.
It's not always a good idea to turn scripts loose on important system files. Make sure you know what you're doing and don't trust any old codger on the net (including me). Test it thoroughly.
To get rid of the marker, use:
sed '/\/as.sh/s/^#//'
This is the opposite operation, it finds those lines containing /as.sh and substitute any # character at the start of the line with nothing.
To add the comment:
sed -e "s/\(.*\)\(as.sh\)/\#\1\2/g"
To remove the comment:
sed -e "s/\(^#\)\(.*\)\(as.sh\)/\2\3/g"
use crontab -e to modify the current user's crontab.