Setup:
Raspberry Pi 2 Model B V1.1
Raspbian Jessie Version March 2016
Hi I am new in the RPI world, I managed to setup 2 DHT22 temperature sensors connected to GPIOs 22 and 23, 2 analog temperature sensors and 1 light sensor connected to a MCP3008 analog to digital converter via hardware SPI. I also managed to send the readings every 5 seconds to a mysql library. I also setup a LAMP server. To send the values to the internet, I make use of the dweepy library to use the www.dweet.io website to eventually create a dashboard in freeboard.io. I was able to accomplish all that. The python code is located at /home/pi/python_programs/sensors.py
.
I want to run the code at startup, I was able to do that only after login as pi by doing this:
sudo nano /etc/profile
Scroll to the bottom and add the following line :
sudo python /home/pi/pythom_programs/sensors.py &
This only works after I SSH to the pi and then login as pi. I want the program to run without any intervention. I have already tried the @reboot in crontab -e, but it didn't work. I know that I have to go through systemd service setup since this is how the latest jessie runs its services (as far as I know). I have seen tutorials that deal with rc.local, but as far as I know, I MUST USE SYSTEMD IN THE LATEST JESSIE. I have tried several instructions online for systemd but I do not know how it works at all. Can somebody please help me.
@steverobillard Thank you for your response, I am also sorry I replied in the comment section. I followed your link and the python program worked partially. The program is supposed to read the sensors, store the values in a mysql database, then send them via internet to the dweet.io website, wait for 5 seconds and start from the beginning again). It reads the sensor, then saves the data in the mysql database, but it doesnt run the dweepy command dweepy.dweet_for('luxerel_rpi', binary,). This is the code for the luxerel.service file that I created:
[Unit]
Description=Luxerel Sensors
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/luxerel_programs/luxmspv1.py & > /home/pi/myscript.log 2>&1
[Install]
WantedBy=multi-user.target
When I test the service by entering sudo systemctl status -l luxerel.service y get the following output http://res.cloudinary.com/luxerel/image/upload/v1460048041/Capture_2_waeqqz.jpg
I think it is due to the network not being ready when systemd invokes the python script. I have seen the Wants and After commands included in the service file, but I do not know the right order, or in which sections to include them.
Please help me.
Thank you
@goldilocks Thank you for your help. I performed the following changes inside /home/pi/luxerel_programs:
$sudo nano startup.sh
Then entered this code:
*#!/bin/bash*
*exec &> /home/pi/myscript.log*
*echo $(date)*
*# Fork/exec*
*(*
*exec /usr/bin/python /home/pi/luxerel_programs/luxmspv1.py*
*) &*
*exit 0*
Then:
$sudo chmod 755 /home/pi/luxerel_programs/startup.sh
$sudo nano /lib/systemd/system/luxerel.service
I entered:
[Unit]
Description=Luxerel Sensors
Requires=local-fs.target
[Service]
Type=forking
GuessMainPID=no
StandardInput=null
ExecStart=/home/pi/luxerel_programs/startup.sh
[Install]
WantedBy=default.target
Then:
$sudo systemctl daemon-reload
$sudo systemctl enable luxerel.service
$sudo reboot
Once again it read everything but did not trigger the dweepy command at startup.
Please help!!!
Thank you!!
------------- UPDATE 04/08/2016 ---------------
@goldilocks, I thought it worked, but it didn't. I performed the following changes:
$ sudo nano /home/pi/luxerel_programs/startup.sh
*#!/bin/bash*
exec &> /home/pi/myscript.log
echo $(date)
*# Fork/exec*
(
*### Optional Code*
target=google.com
limit=100
count=0
while [ $count -lt $limit ]; do
count=$(($count+1))
ping -c 1 $target &> /dev/null
if [ $? -eq 0 ]; then
echo "Online."
break;
fi
echo "Ping $target failed..."
sleep 2
done
echo "$count pings."
*### End Optional Code*
exec /usr/bin/python /home/pi/luxerel_programs/luxmspv1.py
) &
exit 0
Then I entered
$ sudo chmod 755 /home/pi/luxerel_programs/startup.sh
$ sudo nano /lib/systemd/system/luxerel.service
[Unit]
Description=Luxerel Sensors
Requires=local-fs.target
[Service]
Type=forking
GuessMainPID=no
StandardInput=null
ExecStart=/home/pi/luxerel_programs/startup.sh
[Install]
WantedBy=default.target
I also tried adding Requires=network-online.target but it didn't work. For some reason, I think it worked once, but it didn't work again.
Please help me!!
Thank you!!
systemd
service which runs a script but it requires a bit of study, as it is different in concept. See wiki.ubuntu.com/SystemdForUpstartUsers. I findcron
much easier. As you didn't post yourcron
we can't help. NOTE that the script (and everything it calls) NEEDS to be able run without logging in - no matter what method is used.