0

I want to run a script in every X hours, that's why I wrote a BASH (named as mybash.sh) what is calling this script like this :

#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/usr/src/Python-2.7.13 python /usr/src/Python-2.7.13/test1.py

This bash file is in my /usr/src/Python-2.7.13 , and my test1.py too. If I am running the sh mybash.sh it is running! I did chmod a+x mybash too.

Into the crontab-e I wrote that:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/usr/src/Python-2.7.13 
* * * * * /usr/src/Python-2.7.13/mybash.sh 2>&1

If I am calling the crontab-l I am seeing these lines there.

But my script not running..What am I missed?

EDIT1 - cat /etc/crontab

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
Harley
  • 469
  • 1
  • 6
  • 16
  • Why did you wrapped python script into bash script ? – Yaroslav Surzhikov Oct 09 '17 at 06:50
  • Because I read that, it is the usual process to run a .py file with the cron. – Harley Oct 09 '17 at 06:52
  • what does the script do? also, why are you not using the `python` interpreter that comes with Debian? – umläute Oct 09 '17 at 10:06
  • the script is web-scraping (open some websites), how can I use it? – Harley Oct 09 '17 at 10:14
  • does the script require a GUI? (open a browser?). what do you have in `/var/log/cron.log`? what does the user under which the cron-scrip is being executed get in the `${MAIL}` file? (when replying, please use "@umläute" so i get notified) – umläute Oct 09 '17 at 12:29
  • I haven't got /var/log/cron.log (it is not found). ${MAIL}? how can i check it? @umläute – Harley Oct 09 '17 at 12:32
  • `cat ${MAIL}` in your favourite shell (running as the user in question). it simply expands to something like `/var/mail/harley`. If you don't have a `/var/log/cron.log` file, thenprobably your `cron` is not running. Check with `systemctl status cron` – umläute Oct 09 '17 at 12:44
  • systemctl status cron : ACTIVE - RUNNING. (loaded:/lib/systemd/system/cron.service) ; (cgroup:/usr/sbin/cron -f) , but if i type /var/log/cron.log there is nothing. – Harley Oct 09 '17 at 12:52

1 Answers1

0

You could simple invoke python in the crontab

* * * * * [path/to/py/executable] [path/to/py/script]

Remember to specify the complete path to executable due to restricted variables in cron environment.

Wyatt Gillette
  • 318
  • 3
  • 14
  • So i have to write maybe this one? : * * * * * /usr/src/Python-2.7.13/mybash.sh /usr/src/Python-2.7.13/test.py ? – Harley Oct 09 '17 at 10:16
  • nope, i mean, for example `* * * * * /usr/bin/python /usr/src/Python-2.7.13/test.py` – Wyatt Gillette Oct 09 '17 at 10:46
  • no output neither mail log? Are u sure u found the right python path? If u put /usr/bin/python (or whateaver) in a shell, it spawns Python interpreter? – Wyatt Gillette Oct 09 '17 at 11:44
  • i am sure, because my python program is start with #!/usr/bin/python and #!/bin/sh . So i don't know why it cant start – Harley Oct 09 '17 at 12:05
  • so, i cant help you there 'cause it should be good... just a stupid question, have you inserted the blank like at the end of crontab? maybe post the result of cat /etc/crontab here, if it's not too long – Wyatt Gillette Oct 09 '17 at 13:14
  • blank like? what do you mean? – Harley Oct 10 '17 at 08:06
  • the crontab file (/etc/crontab) require a blank (empty) line at the bottom to work properly. Could you please post the result of "cat /etc/crontab" ? – Wyatt Gillette Oct 11 '17 at 07:22
  • Yes, of course. I updated it in my question @Wyatt Gillette! – Harley Oct 11 '17 at 13:35
  • I can't see the ` * * * * * /usr/bin/python /usr/src/Python-2.7.13/test.py ` - like code...you deleted it? If not, you may try to add ` * * * * * /usr/bin/python /usr/src/Python-2.7.13/test.py ` (obviously edited with the right paths) directly in the cron file through ` vi /etc/crontab ` and let me know if it works...after modifying directly crontab, my advice is to restart cron service too. Also, what distro are u using? More, it's a graphical script or console based? EDIT: also, remember the blank line (without #, i don't know if it interfer) – Wyatt Gillette Oct 11 '17 at 14:28
  • oh i found https://stackoverflow.com/questions/4460262/running-a-python-script-with-cron the first answer could help you – Wyatt Gillette Oct 11 '17 at 14:48
  • 1. I tried the first answer..I added the first line to my python script, I made it chmod +x, but nothing changed. 2. If I am typing crontab -e I can see my test.py in the crontab, but you are right, if I am using cat /etc/crontab there isn't there :S It is a web-scrapping script. – Harley Oct 12 '17 at 06:35
  • Probably is set in the local crontab of your user (if im not wrong should be in /var/spool/cron/crontabs/username). I think you tried but i still ask, you could write your line in the general crontab (adding "username" after the time specification, like "* * * * * username /usr/bin/python /usr/src/Python-2.7.13/test.py" . May be helpful to write "root" as username if script requires privileges. One thing more, try to add "1> /dev/null 2> /home/username/error.log" and check if error.log contains useful news. This should redirect stderr (2) to error.log and discard stdout (1). – Wyatt Gillette Oct 12 '17 at 10:03