Run two commands in cron - linux

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.

Related

Cronjob works with regular user but not with sudo [duplicate]

I followed some other posts in stackoverflow and successfully setup cron with RVM using rvm cron setup and injected some ENV to the crontab file.
When I was troubleshooting why the dotenv gem is not working, I realised the following:
I placed my test.rb in file path /home/myuser/ruby/test.rb and had my crontab file as shown below:
* * * * * ruby /home/myuser/ruby/test.rb >> /home/myuser/ruby/output.log
and when I puts the output of the test.rb with Dir.pwd. The output states that the rb is run in the /home/myuser/ directory instead of /home/myuser/ruby directory.
While I had a hotfix by manually changing the path. But I wonder why it is the case.
By default, cron tasks of a user are executed from the user's home directory. In order to execute the script from proper directory, you have to "cd" to it.
Consider changing your crontab to:
* * * * * cd /home/myuser/ruby && ruby ./test.rb >> /home/myuser/ruby/output.log
Good luck!
According to #Pawel Dawczak who left the answer in the comment.
the solution is to rewrite the statement in crontab as
* * * * * cd /home/myuser/ruby && ruby test.rb >> /home/myuser/ruby/output.log
Thanks!

Executing a python script using cron

I have a python script which updates a database. The script reads the update values from another database. The database being read from is updated everyday hence my script needs to run everyday.
After executing crontab -e, then
* * * * * . $HOME/.profile; cd /home/lonwabolap/Dashboard/Web_application && /usr/bin/python3.6 sdb_access_update_night_info.py >> file.out
The file, 'file.out' is empty. The expected output is 'successful (today's date)'. Running it every minute is on purpose. It is intended to run at 8 am everyday. The path to the script is correct. My assumption is that since i used environmental variables for my database credentials it doesn't work.
Please help me find a way to make this script work
Try this:
* 8 * * * . $HOME/.profile; cd /home/lonwabolap/Dashboard/Web_application && /usr/bin/python3.6 sdb_access_update_night_info.py >> file.out

Crontab absolute path not working

I have a script to backup my database at /home/<user>/bin/dbbackup. The script is executable by all users, and owned by me. The files /etc/cron.allow and /etc/cron.deny do not exist.
In my crontab I have the following lines (including a new blank line after the last line of code):
#reboot /home/<user>/.dropbox-dist/dropboxd
30 2 * * * bash /home/<user>/bin/dbbackup
However, cron is not running my dbbackup script. When I run a manual test of the script it works. When I run this test on the command line: * * * * * /bin/echo "cron works" >> ~/file I get the following error:
No command 'dbbackup' found, did you mean:
Command 'dvbackup' from package 'dvbackup' (universe)
Command 'tdbbackup' from package 'tdb-tools' (main)
dbbackup: command not found
My server is running Ubuntu Trusty. Any help please?
As the comments noted, it appears that amiga_os needed remove the reference to bash in the line.
30 2 * * * bash /home/<user>/bin/dbbackup
Should be.
30 2 * * * /home/<user>/bin/dbbackup
I usually just call scripts from their path and use "#!/bin/bash" (or wherever your bash lives) as the first line of the script. It appears the amiga_os had already done this, which is good. I don't like putting sentences into cron because it makes me nervous.
I think it was a path issue as cron executes as the user but does not read the bash profile and therefore does not work exactly like it would under your shell as it might not have access to your $PATH.

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