How to run a .py file using Crontab in Terminal? - cron

I have a python code which must run every hour.
I set Crontab to be:
0 * * * * python /Users.../file.py
Will it actually run the python code? Or Crontab can only do things like 'echo' etc?

Yes, you can run the python code from Crontab here is an example that I use:
First, to open crontab file:
crontab -e
On the last line of crontab file I write the command:
0 */4 * * * python3 /home/integrations/geckoboard/diario1.0.py
Where integrations is my User, geckoboard my folder and diario1.0.py is the code I want to run
With that my pythonfile will run every 4 hours.

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.

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.

Linux system command not working in Perl script via crontab

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.

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

Resources