I tried to schedule crontab for automate kibana dashboard data export, but it's not working.
crontab -e is
* * * * * /home/rehan/Installation/SIEM/ELK%20/Other/test.sh
test.sh file has execution permission but the script is not get run every minute. Any suggestions?
Hi % sign is special character in crontab.Need to escape this with backslash \.
Percent-signs % in the command, unless escaped with backslash \%, will be changed into newline characters, and all data after the first will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the shell's trailing \.
Related
I would like to know how to make a cronjob every 5 minutes ?
I found on Internet that I have to do */5 * * * * but the problem is when I try this I am facing this error
Where is the problem ?
Thank you !
It can be several things:
there are an unwanted control characters in file (you can check with cat -v /etc/crontab or cat -v /var/spool/cron/crontabs/username if this is user crontab entry). Cron daemon has very sensitive parser and unwanted non-printable characters cause errors.
there are empty variable assignments above/under the line erroring (such as MAILTO=)
this is the last line in file and it does not end with newline character. Some implementations of cron (you don't state anything beyond "Debian" tag) require first and last line to NOT be crontab entries.
you have very old cron implementation that doesn't support such */n syntax (afaik Solaris and older busybox cron daemons don't). In that case you need to use workaround and list all 5 minute milestones within one hour like this 0,5,10,15,20,25,30,35,40,45,50,55 * * * * command as a workaround.
It would help if you post a minimal reproducible example of crontab file that errors and version of cron daemon you are using.
want to delete indexInitializer.log file. so define crontab with this:0 3 * * * rm -rf /alidata1/setupTms3/jboss-eap-6.4/%LOG_DIRECTORY%/tire2e/indexInitializer.log.*
However, all files in the folder /alidata1/setupTms3/jboss-eap-6.4/ were deleted during actual operation.
check cron log found : Jul 21 17:00:01 tmsprod4 CROND[26461]: (root) CMD (rm -rf /alidata1/setupTms3/jboss-eap-6.4/), Why is the suffix(%LOG_DIRECTORY%/tire2e/indexInitializer.log.*) missing during execution. There is no problem with running directly.
You need to backslash the % character, so:
0 3 * * * /bin/rm -rf /alidata1/setupTms3/jboss-eap-6.4/\%LOG_DIRECTORY\%/tire2e/indexInitializer.log
From man 5 crontab:
The 'sixth' field (the rest of the line) specifies the command to be
run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file.
Percent signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the shell's trailing "".
I have a strange problem of being to able to run a bash script from commandline but not from the crontab entry for root. I am running Ubuntu 12.04.
* * * * 1-5 root /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
If I run the script from the cmd line using bash, it works fine but sh fails with following error:
> jmeter-cron-randomise.sh: 7: jmeter-cron-randomise.sh: arithmetic
> expression: expecting primary: " % 1 "
Having googled the problem, it seems like standard shell doesn't have the same math operators, like % (modulus), as bash. I'm Not sure why the cron job is failing in the script? I am assuming it is because it's not using the bash shell? It's definitely being fired by the cron daemon (can see it in /var/log/syslog). Any help much appreciated.
You likely need to tell cron that the shell to use is the bash shell as it defaults to sh. You can do that for all crontab entries by putting this line in your crontab:
SHELL=/bin/bash
Note that this will cause all scripts in the crontab to be run under bash which may not be what you want. If you want to change the crontab line itself to just run bash, change it to this:
* * * * 1-5 root /bin/bash /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log 2>&1
Note that I have also caused stderr to be written to the cron.log file (2>&1) which may not be what you want but is pretty common practice. This may help you further diagnose errors from the script.
In case this helps anyone: for me this appeared to be because I had ended up with "DOS" line endings (CR-LF) instead of "unix" line endings (LF). This can be checked using od or your favourite hex dump tool, e.g.:
od -c <script_file>
... and look for \r\n instead of just \n.
It seems (and this article supports it) that the CR character stops the "shebang" from working because it's interpreted as part of the shell executable's filename.
(The line endings themselves appeared because the file came from a git repository and was transferred via a Windows machine).
I also encountered this problem trying to schedule a database backup as root and it made me pull my hair out! I was working on a CentOS 7 box.
Whenever I would check /var/spool/mail/root I would see a log:
sh: root: command not found, yet the command would run perfectly in the terminal.
This is what worked for me:
I created the crontab entry using crontab -e while logged in as root.
Using the command above as an example:
* * * * 1-5 root /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
I deleted the root user entry like:
* * * * 1-5 /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
That solved my problem.
I want to ssh to a machine via proxy server and run a script on that server with the inputs as parameters passed
I am using below :
ssh -t tooladm#200.81.36.188 "ssh -t tooladm#apuatt01" ". ./.profile >/dev/null 2>&1; cd /astxpinfs/ast/tooladm/JHF_SYNC_Particular_HF ; ./SyncToSite.ksh $product $release "${hf_list}" ${LOG_DIR_NAME} 2>&1 > /dev/null"
For clearance :
Suppose I am in machine A, and want to run the script located in machine apuatt01
There is no direct connectivity b/w machine A and apuatt01
So I am connecting apuatt01 via 200.81.36.188
By using this, I am not able to run the above script
Please can you help, where I am doing wrong
You can use Bash here document to make the script cleaner:
ssh -t tooladm#200.81.36.188 -- ssh -t tooladm#apuatt01 <<EOS
source .profile >/dev/null 2>&1
cd /astxpinfs/ast/tooladm/JHF_SYNC_Particular_HF
./SyncToSite.ksh $product $release "${hf_list}" ${LOG_DIR_NAME} 2>&1 > /dev/null
EOS
Note, the double dash separates the command from arguments passed to ssh.
This syntax works in ksh, too. From the KornShell manual(man ksh):
<<[-]word The shell input is read up to a line that is the same
as word after any quoting has been removed, or to an end-of-file.
No parameter substitution, command substitution, arithmetic
substitution or file name generation is performed on word. The
resulting document, called a here-document, becomes the standard
input. If any character of word is quoted, then no interpretation is
placed upon the characters of the document; otherwise, parameter
expansion, command substitution, and arithmetic substitution occur,
\new- line is ignored, and \ must be used to quote the characters \,
$, `. If - is appended to <<, then all leading tabs are stripped from
word and from the document. If # is appended to <<, then leading
spaces and tabs will be stripped off the first line of the document
and up to an equivalent indentation will be stripped from the
remaining lines and from word. A tab stop is assumed to occur at
every 8 columns for the purposes of determining the indentation.
I usually work with BASH, but I'm trying to setup a cronjob from a machine and user account that is configured with zsh. When the cronjob runs, the date variable does not contain the date, just the string for the command to return the date.
DATE=$(date +%Y%m%d)
55 15 * * 1-5 scp user#host:/path/to/some/file/$DATE.log /tmp
I've tried using backticks rather than $() around the command, but that did not work either. Is there a special way to do command substitution in zsh?
If this is a line from the user's crontab, then the line to set the variable isn't set by the shell, it's set by cron itself - and as you've discovered, it doesn't do a lot of interpretation on the values!
The values do get set, but set literal values in the process's environment - and environment variables don't get interpreted further after being expanded - so the $(date) stays as $(date), as you've seen.
The easiest thing to do would be:
55 15 * * 1-5 scp user#host:/path/to/file/`date +%Y%m%d`.log /tmp
The backticks `` are another way to interpolate the result of a command, that works in more shells than $(command) (though it is less flexible, since you can't nest it).
I don't think the cron way of setting up environment variables supports the $() stuff. It doesn't even support variable substitutions.
I think you'll either have to put the date calculation into the cron command:
55 15 * * 1-5 scp user#host:/path/to/some/file/$(date +%Y%m%d).log /tmp
or create another script which sets the variable then calls scp with it (and of course you call that script from within your crontab).