0

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.

  • Have you checked the status and the log of the service? (systemctl status unison-timer-usb-work) – saiarcot895 Aug 3 '15 at 19:17
  • @saiarcot895 : thanks for the tip looks like the problem is this line then? ago 03 22:18:50 mothership unison.sync.usb-work[24820]: Fatal error: exception Util.Fatal("Environment variable HOME not found") the service is loaded and active, I trimmed the output as too long for a comment. Where might I be trying to use the HOME var though? – Tom Rhodes Aug 3 '15 at 20:23
  • Sounds like the unison process depends on HOME. You might want to try running it as a user service instead of a system service. (That way, it doesn't run as root). – saiarcot895 Aug 3 '15 at 20:36
  • @saiarcot895 Ok thanks, I thought seeing as the script ran with or without sudo it would have been fine. I'm trying it as a user service now but I've run in to other problems. I'll open a new Question for that though. If you want to write an answer based on the system/user services and the process requiring an environment variable not available as system then I'll accept as solving this problem :) – Tom Rhodes Aug 4 '15 at 8:09
1

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 and PATH are set by default (at least on Ubuntu 15.04). For the user instance, in addition to the previous variables, HOME, LOGNAME, SHELL, and XDG_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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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