GPhoto and Systemd Issue

The place to post if you need help or advice

Moderators: ChrisThornett, LXF moderators

GPhoto and Systemd Issue

Postby philm36 » Sat Aug 24, 2013 8:43 am

I have a Raspberry Pi with a really useful Adafruit LCD display fitted, which is set up to run Arch. I’ve written a Python script to run at boot which uses gphoto2 to control a digital camera in order to take timelapse photographs. The script allows user to set the number of images to take, and the time lapse between each shot, using the LCD plate.

The script uses the command “gphoto2 –set-config-value” to instruct the camera to save the images on the SD card in the camera. If I run the script under Python (which has to be done with sudo to run the Adafruit modules), it runs correctly and saves the images on the camera. If I run it through systemd with a service script linking to the exact same file, either at boot or starting it with “sudo systemcontrol start gphoto.service”, it executes the command to set the config value, but then appears to ignore it. The script executes without error but the images are not saved. I have added logging to the script which shows the command being executed as below.

Under Python, it shows:

INFO:root:Executing --set-config-value /main/settings/capturetarget=1
INFO:root:Executing --get-config=capturetarget
INFO:root: Label: Capture Target
Type: RADIO
Current: Memory card
Choice: 0 Internal RAM
Choice: 1 Memory card

Under systemd, it shows:

INFO:root:Executing --set-config-value /main/settings/capturetarget=1
INFO:root:Executing --get-config=capturetarget
INFO:root: Label: Capture Target
Type: RADIO
Current: Internal RAM
Choice: 0 Internal RAM
Choice: 1 Memory card

I’ve run a stripped down version on a laptop running Arch, and the same issue occurs. I have also run it under openSUSE on a laptop also using systemd and the same thing happened, so I suspect this is an issue with systemd (or my use of it) rather than the Pi. I would add that I have run the same script under Raspbian (using init script) and this worked correctly both run as a Python script, and at boot as an init script (with the addition of a usbreset code to prevent it from hanging between shots; this code has also been added to the Arch version and the scripts on the two different OS are identical). Below is my service file. Does my problem lie with the (rather basic) service file, or is it elsewhere?

[Unit]
Description=GPhoto Python Timelapse script

[Service]
ExecStart=/home/pi/timelapse.py

[Install]
WantedBy=multi-user.target
philm36
 
Posts: 4
Joined: Wed Aug 21, 2013 7:47 pm

Update

Postby philm36 » Mon Nov 25, 2013 9:00 pm

This was also submitted to the magazine and a solution printed in LXF178.

“This is not an issue with systemd but with the environment in which the script is being run.There is a difference between running a script or program from a daemon such as systemd and from a shell. When you open a shell session, a specific environment is set up - that environment is not present when running from a daemon, even though it is running as the same user. The issue here is not that you are running the script from systemd, but that you are not running it from a shell session.”

The recommendation is to run the Python script within the correct environment by using a wrapper script:
#!/bin/bash
source /root/.bash_profile
/home/pi/timelapse.py

I’ve now resorted to trying to fix this on my laptop before committing it to the Pi. I have Arch on a laptop and on a PC, and /root/.bash_profile did not exist on either. I have tried calling /home/user/.bash_profile within the wrapper, calling /etc/skel/.bash_profile, and creating /root/.bash_profile by copying from both /home/user, and from /etc/skel. In all cases, running the wrapper script in a terminal session causes the script to run correctly and store images on the SD card. If it is run through systemd, the script runs OK but fails to save the images to the SD card.

So I’m currently trawling through anything I can find on bash environment settings. And that’s where I am with this now.
philm36
 
Posts: 4
Joined: Wed Aug 21, 2013 7:47 pm

Postby nelz » Mon Nov 25, 2013 10:42 pm

Do you have no profile files at all in your home directory? What about /etc/profile?
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8497
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby philm36 » Sat Dec 14, 2013 12:16 pm

Sorry for not replying sooner

I have been trying to get my head around sessions and profiles and, after reading through various webpages, including the Arch wiki pages, have tried the following:

1. copying the .bash_profile file from /etc/skel to /root and sourcing it;
2. sourcing the .bash_profile in /home/pi;
3. sourcing /etc/profile

All have the same result - the script executes and triggers the camera to take the required number of shots, but fails to set the config value to save the images on the SD card
philm36
 
Posts: 4
Joined: Wed Aug 21, 2013 7:47 pm

Solution

Postby philm36 » Tue Jul 08, 2014 10:04 am

Finally came back to this and have solved the issue.

The command “gphoto2 –set-config-value” does not change the settings in the camera, but writes them to file .gphoto2/settings in the home directory. When run under systemd, this HOME environment variable was not set (even when using the /root/.bash_profile). I could probably have set this in the profile, but opted to replace the "source /root/.bash_profile" line with "export HOME=/home/pi".
philm36
 
Posts: 4
Joined: Wed Aug 21, 2013 7:47 pm


Return to Help!

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 2 guests