I've got a problem whereby if I run my bash script myself unison works as expected, when systemd runs it via a service on a timer then it does not work as expected. I'm logging the output of the unison executable and it comes back with nothing. What could be happening here?
The script in question is this...
#!/bin/bash
echo "`date -u` `unison "USB Work" -batch`" >> /home/tom/timer.log
...which when run from the commandline works perfectly. The file is /usr/local/bin/unison.sync.usb-work.
My service /etc/systemd/system/unison-timer-usb-work.service looks like this...
[Unit]
Description=Unison timer - sync work to USB
[Service]
Type=simple
ExecStart=/usr/local/bin/unison.sync.usb-work
...the service is run every 10 minutes successfully by the following timer (etc/systemd/system/unison-timer-usb-work.timer)...
[Unit]
Description=Runs Unison USB Work sync script every 10 mins
[Timer]
# Time to wait after booting before we run first time
OnBootSec=1min
# Time between running each consecutive time
OnUnitActiveSec=10min
Unit=unison-timer-usb-work.service
[Install]
WantedBy=multi-user.target
All of this appears to be set up correctly, systemctl list-timers shows it as active...
NEXT LEFT LAST PASSED UNIT ACTIVATES
n/a n/a lun 2015-08-03 08:50:25 CEST 8h ago ureadahead-stop.timer ureadahead-stop.service
lun 2015-08-03 17:00:40 CEST 9min left lun 2015-08-03 16:50:40 CEST 30s ago unison-timer-usb-work.timer unison-timer-usb-work.service
...and the /home/tom/timer.log file gets new blank entries with a timestamp every 10 minutes.
Why does the script work from the terminal (without sudo and with sudo) and not from the timer?!
Thanks in advance for any help,
Tom.
systemd has two "modes": system and user. When systemd is running as a system instance, then everything is, by default, run as root. When systemd is running as a user instance, then everything is run as the user that started the systemd instance. In addition, a limited set of variables are set for services started. For the system instance, only
LANG
andPATH
are set by default (at least on Ubuntu 15.04). For the user instance, in addition to the previous variables,HOME
,LOGNAME
,SHELL
, andXDG_RUNTIME_DIR
are set.In your case, based on the output of the process, the process is meant to be run under a user's account, not as root. The proper fix would be to move the service file to either
/etc/systemd/user
or~/.config/systemd/user
, so that the service would be started under the user's systemd.