I'm doing a project in home automation and it contains a temperature sensor i.e DSB1820 and a gas sensor MQ2 coupled with an MCP3008 ADC. Using RPI2 with Jessie. now, the script which contains 2 python programs,

1st : A python Program which Plots temperature graph using Plotly service. 2nd : A python program which sends an email alert to the user if the MQ2 Gas sensor value is above the set threshold value.

both these programs need internet to perform their tasks, and the problem with using crontab here is, it runs these on Boot giving me an error of no internet connection.

what i want is a script which contains these 2 programs and allow them to run once the Pi has fully booted up and connected to my wifi/lan , to which i set a time limit of 5 mins, can be manipulated later

  • 1
    This is a general question rather than being specific to the Raspberry Pi. I'd use the reboot entry for crontab and run a job with a 5 minute delay at the start. Research cron and crontab.
    – joan
    Mar 26, 2016 at 8:32

2 Answers 2


Here's the script which is going to run the two programs. I'm presuming you want them to run root, there's no indication otherwise and so we'll call that a separate question.


# Redirect standard out and standard error to a file.
exec &> /var/log/whatever.log
echo $(date +"%D %T")" Begin."

    sleep 300
    echo $(date +"%D %T")" 5m elapsed, starting foo and bar."
    /path/to/foo arg1 arg2 &
    exec /path/to/bar arg1
) &

exit 0

Above foo and bar represent your two programs. The parentheses they and everything that takes time (e.g., the sleep) are inside of, with the & (fork) at the end, are critical. This is a subshell. It is an independent, child process. The parent is going to exit right away (last line), leaving it running in the background. Note the output redirection (exec &> ...) applies to it as well. This makes it important that you use bash in the first line (the "shebang", think # = "shh" ! = "bang") and not just /bin/sh, since &> is a bash-ism. They are the same program anyway but how it is invoked affects how it runs.

I presume foo and bar have their own logging arranged; you may also redirect their individual ouput when you launch them above.

This is started with the following systemd service file:

Description=Plotly and Sensor stuff



Put that in /etc/systemd/system and call it local_waitAndPlot.service. Make sure that and the script are owned root, the easiest way to do all this is su root but since user pi has such broad permissions you could do it that way and use chown. The script must be executable: chmod 755 script.sh. The service file doesn't, the default mode should be fine.


sudo systemctl enable local_waitAndPlot.service

Obviously you don't need the sudo if you already su root. Anyway, it should mention something about adding a symlink. Check inside /etc/systemd/system/default.target.wants and you should see it there.

Reboot. You should be able to immediately check /var/log/whatever.log and see that "Begin" statement. Five minutes later you'll see the other one and hopefully foo and bar start with no problems. Pay attention to the structure there, namely the & on the first process and the exec on the second. This is very simple but necessary stuff.

The script will occupy < 1 KB RAM and use zero processor time for the five minutes it waits. Details about the date format are in man date, about the service file entries see man systemd.service and man systemd.unit. Systemd itself has timers, if you research that you may be able to do it that way, see apropos systemd and more specifically man systemd.timer -- I've never used one for anything and you probably need the launcher script anyway.

  • going to try this one out , will update once i have the results! Mar 27, 2016 at 19:42

As Joan wrote: This is a general question.

I'd write a bash-script like this:



sleep $Seconds

nohup /usr/local/bin/script1.py &
nohup /usr/local/bin/program2.py &

  • this too looks promising! all i have to do then is run this specific script in the crontab right? without any extra options as such? Mar 27, 2016 at 19:43
  • Yes, that shall do it. Though goldilocks' is way more elaborated.
    – kleinski
    Mar 31, 2016 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.