0

So I want to save some power and let the raspberry pi download my files instead of my PC which is a power monster

I want to start my downloads at 3am and pause them at 7am. I tried to use crontab but it never works

How can I do this :?

I've written a python script to do this for me but I'm not sure how to always execute it at startup or leave it running because I only use SSH. I'm not sure if it's ok to always have this script running in terms of cpu usage or other safety stuff! (it just executes wget and sends terminate signal if its 3am or 7am, I have a file on desktop which i add my links too thought ssh)

I have archlinux installed on it!

here's my crontab which doesn't work:

0 3 * * * wget -c -i /home/mename/Desktop/downloads.txt
0 7 * * * killall wget

edit:

still doesn't work, i made this job * * * * * echo "WAIT WHAT?" > /home/alarm/Desktop/Iscromwell.list adding this to SU works (running crontab -ewith sudo) isn't this dangerous :? why doesn't it work as a normal user!?

Edit:

I moved this to Linux section as this is more Linux related now. Things got serious! I have no cron service running and i get an error trying to run it

user3033693
  • 101
  • 3
  • 1
    cron was made for stuff like this. You could leave the python script in an infinite loop that checks the time every 5 minutes and goes back to sleep but it's a hackish solution. Figure out why cron isn't working instead. Did you remember to chmod +x your_script? With and without sudo? Did you try adding your script to root's crontab as well as your user's crontab via crontab -e / sudo crontab -e? (only add the script to one of the crontabs but test both - one at a time). Add a pid file feature or some other locking mechanism to the script to prevent cron from starting multiple instances – jDo Mar 15 '16 at 13:29
  • If you want a clearer answer than what Gaurav Dave gave, you are going to have to include the actual crontab line that you claim does not work. Right now your question reads like, "I tried to turn a light on but it didn't work. What should I do?" -> We don't even know if you are using the correct switch. – goldilocks Mar 15 '16 at 13:32
  • I did try con without any script at all, let me paste what i wrote into the file here. Plus it was only from my user, ran SU and tried it added it to su crontab too. i will wait tonight. I hope it doesn't get executed two time :| – user3033693 Mar 15 '16 at 14:19
  • @goldilocks I'm asking how to turn the light on :D – user3033693 Mar 15 '16 at 14:35
  • OK using sudo worked, any idea why :? isn't it dangerous? – user3033693 Mar 15 '16 at 14:36
  • Possibly because you did not use the system crontab. You may find this useful: http://raspberrypi.stackexchange.com/questions/40493/log-output-of-background-or-boot-script – goldilocks Mar 15 '16 at 14:38
  • @goldilocks the echo dummy task worked but my wget still doesn't :( – user3033693 Mar 15 '16 at 23:33
  • @goldilocks ok Weird, now not even my dummy works, no idea why it worked. maybe i forgot to remove the file And thought it was the dummy? echo "WTH IS WRONG WITH YOU CRONTAB" > /home/alarm/Desktop/DownloadedFiles/IHATEYOU.TXT is my new dummy, its at 52 19 * * * which is 2 minutes from now (7:50 PM) and It didn't work :( – user3033693 Mar 16 '16 at 16:23
  • My biggest piece of advice with stuff involving cron or init is to just do whatever it is you want, whether it is is a dummy test, a simple command, whatever, in a script, make sure that script is appropriately executable, and use the full path. Do not include anything else in the cron/init entry. Do no include redirection of any kind (&>). Do not include sudo. Do not even include arguments. You can put all that stuff in a short, simple shell script and your life is much easier. – goldilocks Mar 16 '16 at 16:32
  • If you cannot get a simple script with, e.g., echo "hello world" > /tmp/test_this.txt to work then you should explain all that in detail over at Unix & Linux. By detail I do not mean "I am trying to do this for my project...", "I am using a raspberry pi and...", etc. Just linux will do fine for context with cron. The detail is: 1) The exact text of the script your're trying to execute. 2) The complete output of stat run on this script. 3) The entire content, and the exact path, of the crontab you put an entry into to execute this script. – goldilocks Mar 16 '16 at 16:32
  • @goldilocks stat, no idea what it is! – user3033693 Mar 16 '16 at 16:54
  • See man stat and try it -- stat myscript.sh. It won't do anything to it. My reason for that is it is provides evidence of who owns the script and what permissions are on it (there is still a caveat with access if part of the path the script is in is not readable by a user that could otherwise use the script, but never mind that, presuming this is the system's root powered cron). The point of stuff like this is many people (including me, pretty often) will just skip over stuff where it is obvious too much discussion and "we need this information", etc. has to go on... – goldilocks Mar 16 '16 at 17:07
  • ...It has to do with demonstrating a best effort to solve/identify the problem yourself. It may be somewhat influenced by the fact that I (and many others) have to go to mailing lists to solve problems, where, "Well, we need to know..." discussion could take days or weeks. This is not (no offence) twitter. How to Ask Questions the Smart Way <- An internet classic. – goldilocks Mar 16 '16 at 17:08

6 Answers6

1

You have to write a cron job to do that task.

  1. Write crontab -e in your terminal to access crontab
  2. Then follow below image to write a cronjob.

enter image description here

In the you have to write source\to\python file.py to execute at a given time.

For more information refer this link.

Gaurav Dave
  • 200
  • 1
  • 2
  • 15
  • Thanks lot for you answer, I've done this but for some reason it doesn't execute it! – user3033693 Mar 15 '16 at 14:18
  • for now I manually run nohup name & and it sounds like it will work! because i see my script running in ps ax – user3033693 Mar 15 '16 at 14:19
  • May be your cron is not running properly because of some error, it would be good if you create a cron log, by that you can easily trace the issue. – Gaurav Dave Mar 16 '16 at 04:55
0

I'm no bash ninja but I just wrote this and it seems to work. Edit the cron entry line and the wget command as needed. Add everything to a shell script and run it.

if [ -f /home/$USER/cron_downloads/crontab_$USER.bak ]
then
    echo Already added cron entry. Exit to avoid duplicate cron entries
else
    echo The file does not exist. Continue...

    # make a new directory called cron_downloads to avoid 
    # overwriting existing scripts in home dir
    mkdir /home/$USER/cron_downloads

    # enter the new dir
    cd /home/$USER/cron_downloads

    # write shebang to a new script
    echo '#!/bin/bash' > download_script.sh

    # append wget command to the new script
    echo "wget -c -i /home/$USER/cron_downloads/urls_to_fetch.txt -o /home/$USER/cron_downloads/download_script.log" >> download_script.sh

    # set executable bit
    chmod +x download_script.sh

    # add a URL to the file that's read by wget
    echo "http://example.com" >> urls_to_fetch.txt

    # backup existing user crontab
    crontab -l > crontab_$USER.bak

    # copy backup so we can safely add entries and leave the backup untouched
    cp crontab_$USER.bak crontab_$USER.tmp

    # add a new entry to the temporary crontab. Set schedule as needed
    echo "* * * * * /home/$USER/cron_downloads/download_script.sh" >> crontab_$USER.tmp

    # replace existing crontab with the contents of crontab_$USER.tmp
    crontab crontab_$USER.tmp

    # remove the temporary crontab. Leave the backup
    rm crontab_$USER.tmp

fi

To undo all changes to cron (leaving the newly created directory intact):

# undo changes
crontab /home/$USER/cron_downloads/crontab_$USER.bak
  • Add download urls (one per line) to /home/$USER/cron_downloads/urls_to_fetch.txt
  • Wget activity is logged to /home/$USER/cron_downloads/download_script.log

Output:

I placed the shell commands in a script called add_cron_download_entry.sh

pi@rpi ~ $ sudo chmod +x add_cron_download_entry.sh 

pi@rpi ~ $ sh add_cron_download_entry.sh 
The file does not exist. Continue...

pi@rpi ~ $ while :; do ls /home/pi/cron_downloads; sleep 2; done
crontab_pi.bak  download_script.sh  urls_to_fetch.txt
crontab_pi.bak  download_script.sh  urls_to_fetch.txt
crontab_pi.bak  download_script.sh  urls_to_fetch.txt
crontab_pi.bak  download_script.sh  urls_to_fetch.txt
# BELOW WE SEE THAT WGET CREATED A LOG
crontab_pi.bak  download_script.log  download_script.sh  urls_to_fetch.txt

pi@rpi ~ $ cat cron_downloads/download_script.log 
--2016-03-16 17:57:02--  http://example.com/
Resolving example.com (example.com)... 93.184.216.34, 2606:2800:220:1:248:1893:25c8:1946
Connecting to example.com (example.com)|93.184.216.34|:80... connected.
HTTP request sent, awaiting response... 416 Requested Range Not Satisfiable

    The file is already fully retrieved; nothing to do.
jDo
  • 516
  • 2
  • 6
  • Thanks a lot @jDO I ran it but it doesn't work :( maybe i should run it as sudo, but I don't see why it's ridicules! I want to run it as a normal user :S – user3033693 Mar 16 '16 at 16:39
  • No need for sudo unless you're reading/writing to/from files/directories you shouldn't touch. Did you follow my instructions to the letter? (edit the cron and wget lines in my script) Does your crontab contain multiple entries that conflict? (e.g. calling the same script multiple times simultaneously) – jDo Mar 16 '16 at 16:49
  • no. and I did :| I'm moving this to http://unix.stackexchange.com/ – user3033693 Mar 16 '16 at 16:50
  • Ok, it works perfectly on my pi. I edited my answer so you can see the output – jDo Mar 16 '16 at 17:01
  • hah, weird ~jDO. what OS do you have, raspian? I installed arch to learn a little about linux but I'm starting to regret it :D – user3033693 Mar 16 '16 at 20:37
  • @user3033693 Yeah, Raspbian. I never tried Arch but I've heard enough about it to suspect you jumped in at the deep end :D – jDo Mar 16 '16 at 20:40
  • yeah :D @jDO . I wish there was a book that would give you some Linux general knowledge :D – user3033693 Mar 16 '16 at 20:45
  • @user3033693 There are thousands of books, I'm sure. Counting only the free ones you can find online, there are probably still hundreds. I can't recommend any specific books but would simply search for it. Anyway, what output did you get when you ran my script? Did you add some download entries to /home/$USER/cron_downloads/urls_to_fetch.txt? - replacing $USER with your username. Do you now have a folder in your home directory called cron_downloads? (the script creates that folder if it doesn't exist already) – jDo Mar 16 '16 at 20:59
0

Try a python (.py) file. Running it through Python should work if you can get the correct modules to be able to download them. The language is very easy and shouldn't be much of a task to learn.

Update:

Should have probably read all of your post first. Once you get connected to the Pi via SSH, you should be able to do python pythonscript.py. If you don't have it in the default directory (/home/pi), you'll need to do cd /home/pi/... and so on. I would also suggest implementing SMTP which allows you to email the files to yourself, which you can do by doing a check to see if they have completed or not. You could also add a crontab where on startup or reboot, the python file executes automatically.

Dylan
  • 411
  • 1
  • 3
  • 18
0

"I've written a python script to do this for me but I'm not sure how to always execute it at startup"

If this is your only problem, then you can always write a line in your crontab to specify that it should run a specific program at boot time like this:

@reboot /home/user/download.py

This runs the program as the user, who owns the crontab.

Gaurav Dave
  • 200
  • 1
  • 2
  • 15
Joppe
  • 196
  • 3
0

I suggest invoking wget this way:

wget -b -v -c -i /home/mename/Desktop/downloads.txt -o /tmp/wget.log

instead of the way you are doing it. The extra switches, from man wget, are:

  -b
  --background
       Go to background immediately after startup.

That is probably the critical one, if the problem is solved.

   -o logfile
   --output-file=logfile
       Log all messages to logfile.  The messages are normally reported to standard error.

And finally:

   -v
   --verbose
       Turn on verbose output, with all the available data.  The default output is verbose.

Which seems redundant since that's the default, but sometimes I am dubious with wget (and why would they have a switch to turn on a default feature?).

There is also -d if you still aren't getting anything in /tmp/wget.log. Beware that /tmp is a volatile directory in RAM on most systems, so if you turn the pi on and off you will lose that information.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
0

My crontab service wasn't even running, I used systemctl and fixed it :) systemctl start cronie.service

user3033693
  • 101
  • 3