run script every n minutes without editing crontab file manually - linux

Is there a command that can edit crontab ?
( I don't want to edit it manually )
For example :
command -parameter * * * * * script

The command crontab file will set the crontab to the contents of the given file - if the lines in it are properly formatted (otherwise it will just give an error).
You can use - instead of a filename to read from the standard input, e.g.:
echo "* * * * * script" | crontab -

Related

My php cron doesn't trigger

This is my crontab file (after crontab -e):
50 6,14,22 * * * php /var/www/web_hdef/public/artisan run:handler
0 6,14,22 * * * php /var/www/web_hdef/public/artisan run:endomondo
30 6,14,22 * * * php /var/www/web_hdef/public/artisan run:update
but it doesn't run and after crontab -l in command line I got:
hp /var/www/web_hdef/public/artisan run:updatedo(8)
I ran commands separately a they works
Most likely php executable is not in $PATH of user running the script. Use absolute path
in nano editor I didn't see all of signs in text file, when I openned it in vim editor, I saw that there are extra white spaces. I removed it and now it works.

How to let Linux execute a php file per 5 minutes thread safety?

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.

Run two commands in cron

I need to run a task every hour . I first change directory to the path where script is and then operate that script. So I try to use a cron job as :
59 * * * * cd /home/sansal/Scripts && sudo ./usbreset /dev/bus/usb/002/003
I added that line to crontab. But I cant make sure if it is true. And I dont see any output in terminal about that.
Using the full path is defintely better then first using cd. To get the result of the cronjob, you could just output to file like this:
59 * * * * /home/sansal/Scripts/usbreset /dev/bus/usb/002/003 &>> /home/sansal/usbreset.log
You can test if the script failed with ||
59 * * * * /home/sansal/Scripts/usbreset /dev/bus/usb/002/003 || echo "usbreset failed"
cron automatically sends email with any output of the command.

crontab not working properly

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

Crontab no error but doesn't execute script

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.

Resources