When I run a script manually using root it is working fine. when I execute the same script inside cron it is not running. I checked the cron is actually calling the script but the script is not executing. I exported the output of echos in the script to the text file but the text file is didn't logged anything. Please check this cron
*/10 * * * * sh /var/www/sym_monitor/restart.sh > /var/www/migrate/root_restart.txt
Another approach to avoid typing full executable paths is to put shell global variables at the top of your crontab :
SHELL=/bin/sh
PATH=/bin:/usr/bin:/usr/local/bin
MAIL=me#domain.tld
*/10 * * * * stuff > log 2>&1
Note the > log 2>&1 syntax to log both STDERR & STDOUT in log file
Try changing sh for /bin/sh.
*/10 * * * * /bin/sh /var/www/sym_monitor/restart.sh > /var/www/migrate/root_restart.txt
Related
when calling
/home/username/temp/build/appname 2> /home/username/temp/log/stderr 1> /home/username/temp/log/stdout
both stderr is redirected to /home/username/temp/log/stderr, and stdout is redirected to /home/username/temp/log/stdout.
However, when adding a cronjob
5 * * * * username /home/username/temp/build/appname 2> /home/username/temp/log/stderr 1> /home/username/temp/log/stdout
The application runs as expected, stdout is redirected to /home/username/temp/log/stdout, but stderr is empty.
Any ideas?
EDIT:
The top of /etc/crontab is
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
The issue seems to be exclusive to my (commandline qt) application.
The following cronjob works with correct redirection of stderr:
* * * * * username ls /doesnotexist 2> /home/username/temp/log/ls_stderr
Depending on your distribution it might be that crontab is using a different shell than your are expecting. Two solutions come to mind:
Set the shell in crontab
Add SHELL=/bin/bash (or whichever shell you prefer) to the top of your crontab
Use the redirect syntax of the shell crontab is using
You can find the shell by adding
* * * * * echo $SHELL > $HOME/cronshell
to your crontab. It will most likely be /bin/sh, which again depending on your distribution might be symlinked to a different shell. Here you can find a great summary of different shell redirection syntaxes.
I have a scree.php file in the path:
xxx/.../Home/scree.php
I want to execute it per 5 minutes, and make sure it is thread-safety. How to do that?
I am under XShell.
It can be done using crontab.
Type "crontab -e" in your terminal.
Paste in the following code:
*/5 * * * * php /Home/scree.php
Exit out of your editor.
Learn more about crontab here.
You can edit the crontab in XShell:
crontab -e
Then in the file, you should add the code:
*/5 * * * * root usr/bin/php xxx/.../Home/scree.php
Then esc :wq out of the file.
If it shows:
crontab: installing new crontab
means success.
I have added a script in crontab for every 30 minutes. The line goes as follows:
*/30 * * * * root perl /root/perl.pl
The above script has a execution of system command 'top' and it gets printed in a log file.
If I run it manually it runs fine. But while running it via crontab, it does not show up the desired results. Please can somebody help me with this. Thank you.
The command in the above perl script is:
$top = `sudo top`;
The error I am getting is:
sudo: sorry, you must have a tty to run sudo
I changed my command from sudo to visudo. But still the problem remains.
You should use the full path in cron (and use which perl to find the full path):
*/30 * * * * root /usr/bin/perl /root/perl.pl
OR better yet, make the script executable using chmod +x, and add the interpreter to the beginning of the script #!/usr/bin/perl -w and call it directly from cron
*/30 * * * * root /root/perl.pl
Also if there is a problem in the perl script, you could output the result from cron like this
*/30 * * * * root /root/perl.pl > /tmp/myscript.log
You should look into the requiretty setting with regard to visudo. Look for a line that reads Defaults requiretty. You could try commenting it out, but you will be sacrificing some security. See man sudoers.
You could also try running top in batch mode with one iteration:
$top = `sudo top -bn1`;
Batch mode option is for sending output to other programs.
I have a perl script which Im planning to run every minute.
I have set the cron job as
* * * * * PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl
I assume the script is executing every minute only because I see a entry like below when I do cat cron in /var/log/
Jul 26 04:57:01 dmvbu-build crond[773]: (root) CMD (PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl)
Jul 26 04:58:01 dmvbu-build crond[687]: (root) CMD (PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl)
But my problem is I have statements like
print LOG "connecting to website\n"
where LOG is a file descriptor to a file named log.txt which is located at
dm2/www/html/isos/pre5.3/ (same place as autoDownload.pl)
But I dont see this log.txt file updating with new informations after I see the entry in the cron log file
But I see this file updating when I run the code manually
You have to remove the extra space after the = in PATH.
PATH=/usr/local/bin:/usr/bin:/usr/sbin:/usr/lib
The cron line should be
* * * * * PATH=/usr/local/bin:/usr/bin:/usr/sbin:/usr/lib perl /dm2/www/html/isos/pre5.3/autoDownload.pl
Note the lack of a space after the = and the lack of a semicolon before perl.
Provide the absolute Path to the logfile (/dm2/www/html/isos/pre5.3/log.txt instead of just log.txt) when open()-ing, otherwise you have to ensure that the "current working directory" of cron is where you want it to be.
Also check that the user under which this command is executed has write-permissions to the file.
Its much easier to debug a program when you have the output/errors.
You should also use an absolute path to perl in either the top of you script or on the cron line. You should then be able to get rid of any $PATH messyness.
# Make sure you script is executable
chmod a+x /dm2/www/html/isos/pre5.3/autoDownload.pl
Try adding a MAILTO line to cron above your process.
MAILTO="me#example.com"
* * * * * /usr/bin/perl /dm2/www/html/isos/pre5.3/autoDownload.pl
or alternatively logging the cron output to a file to watch for errors. Get rid of /usr/bin/perl in the crontab by making sure its the 1st line of the script #!/usr/bin/perl.
* * * * * /dm2/www/html/isos/pre5.3/autoDownload.pl &> /tmp/autoDownload.log
I'm trying to execute a shell script from cron on Freebsd.
To test whether crontab is working at all, I wrote the line
* * * * * echo "Hello" > /home/myuser/logile
and it work fine.
But when trying to execute any script it doesn't do anything, not even an error. (In the script I tried to run is just the same echo command)
Below is the output of crontab -l:
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
HOME=/home/myuser
MAILTO=myuser
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile
Why is the script not getting executed, although crontab is obviously running?
Permission for all files are set to rwxr-xr-x.
* * * * * /bin/sh /home/myuser/shellscript.sh
or
* * * * * /bin/bash /home/myuser/shellscript.sh
worked for me in Macosx 10.6 as rootuser
Have you checked that the command line has a linefeed/CR at the end of the line? I struggled for hours trying to find a reason for non-executing php script on cron when I simply hadn't pressed enter at the end of the line when I edited the cron jobs with crontab -e :-)
Have you checked /var/log/cron for clues?
Have you tried
* * * * * /bin/sh /home/myuser/shellscript.sh > /home/myuser/logfile
cron sends any errors via email to owner of the crontab file (often "root" so you might check that account's email). To have any errors mailed to "crontabOnFreebsd" put:
MAILTO=crontabOnFreebsd
in your crontab (near the top).
For more info issue this command:
man 5 crontab
If you are getting an error, then your logfile might not capture it, try this:
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile 2> /home/myuser/errorfile
Its been a while since I did any cron stuff; but things that always used to get me:
Environment variables not been set: generally I found it necessary to set up full paths (even to things like 'cat') to all commands [or at least set ENV variables within the script itself].
Ensure the user who owns the script etc is really the user which is running the script: this might not be the same user when you test from the interactive shell. Same goes for the directories/files that the script might write to.
Also: check the email for the root user - you might find that the errors have been diverted to the inbox, which may help you troubleshoot this further.