-1

Do I need put " python" infront for the .py if write a crontab for it? This is how I do it - */10 * * * * /home/TwitterFollowBot/python bot.py is this correct? and I need to run it every 10 minutes. And will it work even after closing the terminal?

Nov 26 07:12:01 ip************* CRON[3180]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:13:01 ip************* CRON[3190]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:14:01 ip************* CRON[3195]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:15:01 ip************* CRON[3211]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:16:01 ip************* CRON[3226]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:17:01 ip************* CRON[3243]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:17:01 ip************* CRON[3244]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Nov 26 07:18:01 ip************* CRON[3275]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:19:01 ip************* CRON[3278]: (root) CMD (python /home/TwitterFollowBot/bot.py) Nov 26 07:20:01 ip************* CRON[3282]: (root) CMD (python /home/TwitterFollowBot/bot.py)

1 Answers1

1

How to execute a python file without calling "python" directly

If you want to execute a python file directly set the file to executable (e.g. use chmod +x bot.py) and add a shebang on the first line of bot.py that looks something like this:

#!/usr/bin/env python

Then you can execute the python file directly by calling `/home/TwitterFollowBot/bot.

The (likely) cause of the error

I am guessing your error might be because you are trying to execute a python that does not exist. You have:

/home/TwitterFollowBot/python bot.py

Your command it trying to execute a python binary in the /home/TwitterFollowBot directory which probably doesn't exist (unless you installed python there). Maybe you mean to execute:

python /home/TwitterFollowBot/bot.py

This will execute the python found on the path with the absolute path to your python file as its argument.

krock
  • 28,904
  • 13
  • 79
  • 85
  • so let me get cleared, all I have to do is add ( #!/usr/bin/env python ) top of the bot.py and make the crontab like this? */10 * * * * /home/TwitterFollowBot/bot.py and one more thing, is this the correct way to restart it every 10 minutes? */10 * * * * /home/TwitterFollowBot/bot.py – Isuru Nuwan Subasinghe Nov 26 '16 at 05:41
  • Yes, the shebang (#!) describes how the program should be run `/usr/bin/env python` will find the python installed on the system and run it. */10 looks correct to run every 10 minutes. – krock Nov 26 '16 at 05:48
  • Thanks a lot. One last thing, as you mentioned above do i have to put "python" before "/home/TwitterFollowBot/bot.py" ? like this "python /home/TwitterFollowBot/bot.py" or just "/home/TwitterFollowBot/bot.py" – Isuru Nuwan Subasinghe Nov 26 '16 at 05:52
  • 1
    If you added the correct shebang and made it executable correctly (test by just running the script on the command line as "/home/TwitterFollowBot/bot.py", then you don't need the "python" before it in the crontab either. – pidge Nov 26 '16 at 06:12
  • I tried it like you said but still it's not working :( – Isuru Nuwan Subasinghe Nov 26 '16 at 06:54
  • Have you had a look in your cron log file to see what happens when it tries to start? – krock Nov 26 '16 at 07:03