2

I want a python webscraping program to be run everyday at a certain time. For that i am using this command in the cron in ubuntu

28 22 * * * root /home/ahmed/Desktop python hello.py

It just doesnt work. there must be something wrong with it. can anyone help me please?

user3415869
  • 93
  • 2
  • 8
  • 1
    Does it work when you manually issue `/home/ahmed/Desktop python hello.py` command? Probably you need something like `28 22 * * * root python /home/ahmed/Desktop hello.py` – Selcuk Mar 13 '14 at 18:37

2 Answers2

2

Try adding #!/usr/bin/python (called shebang line) to the top of your Python script and then

28 22 * * * root /home/ahmed/Desktop/hello.py

You have to make your script executable like this (run this as a separate command): sudo chmod +x /home/ahmed/Desktop/hello.py

From the Shebang page on Wikipedia:

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.[8] For example, if a script is named with the path "path/to/script", and it starts with the following line: #!/bin/sh then the program loader is instructed to run the program "/bin/sh" instead (usually this is the Bourne shell or a compatible shell), passing "path/to/script" as the first argument.

If you don't want to change anything this will work as well:

28 22 * * * root python /home/ahmed/Desktop/hello.py

vaultah
  • 36,713
  • 12
  • 105
  • 132
  • # is supposed to be a comment in the python script, isnt it? – user3415869 Mar 13 '14 at 18:42
  • `#` is a comment *in Python*, yes but [`#!` has special meaning to *the OS*](http://en.wikipedia.org/wiki/Shebang_(Unix)). You'll also need to ensure that `/home/ahmed/Desktop/hello.py` [has the executable bit set](http://en.wikipedia.org/wiki/Chmod). – Johnsyweb Mar 13 '14 at 18:45
  • @user3415869 Updated my answer – vaultah Mar 13 '14 at 18:46
  • I put the #!/usr/bin/python in the python script (which is recognized as a comment by the text editor) and then ran the command as u mentioned, but still it doesnt work – user3415869 Mar 13 '14 at 18:49
  • @user3415869 you also have to make your script executable: `sudo chmod +x /home/ahmed/Desktop/hello.py` – vaultah Mar 13 '14 at 18:50
  • i did it but still doesnt show anything in my database :( – user3415869 Mar 13 '14 at 18:55
  • @user3415869 then the problem is in the script itself, and not the way you run it – vaultah Mar 13 '14 at 18:56
  • Am i doing something correct? i intend to run a python script in the background automatically at a specific time. the program is connected to my local server's database and inserting values in that. i am checking whether the program is running or not by checking the database(that whether the values are being inserted or not) – user3415869 Mar 13 '14 at 18:57
  • no the script is correct. when i run it without cron it runs well – user3415869 Mar 13 '14 at 18:58
  • @user3415869 Okay, did you try to run it this way: `28 22 * * * root python /home/ahmed/Desktop/hello.py`? – vaultah Mar 13 '14 at 18:59
  • yes . i used this command 02 23 * * * sudo chmod +x python /home/ahmed/Desktop/hello.py – user3415869 Mar 13 '14 at 19:03
  • @user3415869 note: crontab doesn't immediately run your script. Consult the manual (http://kb.iu.edu/data/afiz.html). Crontab **will** launch this script at 22:28 every day. – vaultah Mar 13 '14 at 19:05
  • @user3415869 this is wrong. `02 23 * * * sudo python /home/ahmed/Desktop/hello.py` is correct – vaultah Mar 13 '14 at 19:06
  • working now! Thanks! Can u guide me with a command which runs the script every second? and secondly, will this program run when my computer is turned off? – user3415869 Mar 13 '14 at 19:07
  • @user3415869 The program won't work if your computer is turned off. This is how to run the script every n seconds : http://stackoverflow.com/questions/1034243/how-to-get-a-unix-script-to-run-every-15-seconds – vaultah Mar 13 '14 at 19:09
  • is there any way i can run the program every minute forever regardless of my computer is turned on or off? – user3415869 Mar 13 '14 at 19:11
  • @user3415869 there is no way to do it, I'm assured – vaultah Mar 13 '14 at 19:12
2

/home/ahmed/Desktop is (most probably!) not a valid command name. You want

28 22 * * * root python hello.py

or possibly

28 22 * * * root python /home/ahmed/Desktop/hello.py

depending somewhat on why you put that folder name there.

The syntax of a regular user's crontab is different. I can imagine no legitimate reason to run a scaping program as root. To run it from your own crontab you should use

28 22 * * * python /home/ahmed/Desktop/hello.py

(again possibly without the path name, or with the path somewhere else in the command line).

tripleee
  • 139,311
  • 24
  • 207
  • 268