Executing a python script using cron - python-3.x

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

Related

Can't get Crontab jobs to work at all [duplicate]

This question already has answers here:
CronJob not running
(19 answers)
Closed 4 years ago.
New to Crontab use, I am trying to get a simple bash script to run.
add_temp:
#!/bin/bash
rnd1=$RANDOM
range1=20
let "rnd1 %= $range1"
rnd2=$RANDOM
range2=50
let "rnd2 %= $range2"
echo $rnd1
echo $rnd2
cd /var/www/html
sqlite3 test.db <<EOF
INSERT INTO temps (date, temp) VALUES ($rnd1, $rnd2 );
EOF
crontab -e:
SHELL:/bin/bash
* * * * * /var/www/html/add_temp
This doesn't seem to run at all. Works fine if run manually with /var/www/html/add_temp.
My guess is that the sqlite3 is not found when the cron daemon run the script.
You could modify the script and provide the full pathname of this command. Use a terminal to display the full pathname of the command:
type sqlite3
The cron daemon which will run the script does not have the exact same environment than the bash login shell used to run manually the script.
The variable that is usually differently set is PATH.
The first step to troubleshoot this situation is to compare the environments and debug the script.
I suggest to insert these lines below, after the very first line of the script
env
set -x
Run the script manually and redirect output, from a terminal:
/var/www/html/add_temp >/var/tmp/output_m.txt 2>&1
Change the crontab line for:
* * * * * /var/www/html/add_temp >/var/tmp/output_c.txt 2>&1
Wait that the cron daemon executes the script and compare the /var/tmp/output_m.txt and /var/tmp/output_c.txt files.
When the script will be fixed, remove the 2 debug lines from the script and restore the crontab original content.

Trying to run script through crontab which won't work

I'm having issues getting my crontab to run I have the following line added to my crontab -e but it won't start. The command runs fine if I run it manually.
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py
Not getting any error messages and can't see the process when I run top or grep for it.
Usually this happens because the cron environment is different from your own. Make sure your start.py script uses full paths to any referenced files or external scripts. Make sure that your start.py script does not rely on environment variables that you have in your shell but it may not. Try piping the cron output to a mail command so you can see what it is doing, like so:
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py | mail -s "cron output" myself#example.com
An easier way to troubleshoot this is to write a wrapper shell script and send the output to a log file.
Create file python_start_cron.sh with contents
#!/bin/bash
cd /home/sam/p/ && /usr/bin/python3.5 start.py
Set the execute bit on this script script and make sure the script works manually
Modify the cronjob as shown below
0 */3 * * * python_start_cron.sh >/tmp/python_start_cron.log 2>&1
After cron executes, check the contents of the log file to ascertain the cause of the problem.

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 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.

Cronjob to run hourly

I have written a small bash script which clears the logs (say that script name is clearLogs.sh).
My task: To run the bash script on an hourly basis to clear the logs
What I have done so far: I have created a symbolic link of my bash script and placed it in /etc/cron.hourly.
example -> cd /etc/cron.hourly
ln -s /home/sam/clearLogs.sh clearLogs.sh
Now, an hour has passed but the logs have not been deleted. If I run a script standalone it works as expected. Can you guys please let me know what I am doing wrong here.
1.in clearLogs.sh add line: RUN_PATH=/you/script/path cd $RUN_PATH
2.cat "* */1 * * * /you/script/path/clearLogs.sh" >> $HOME/crontab.txt
3.crontab $HOME/crontab.txt
4.crontab -l,look all crontab job

Resources