# How to make my raspberry pi download my files at specific time?

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

• 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

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.

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

• 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 4:55

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.

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.

"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.

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

# 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

# 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.

• 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 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

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