5

I want to run a script at system startup in a Debian 9 box. My script works when run standalone, but fails under systemd.

My script just copies a backup file from a remote server to the local machine:

#!/bin/sh
set -e

/usr/bin/sshpass -p "PASSWORD" /usr/bin/scp -p [email protected]:ORIGINPATH/backupserver.zip DESTINATIONPATH/backupserver/

Just for privacy I replaced password, user, and paths above.

I wrote the following systemd service unit:

[Unit]
Description=backup script

[Service]
Type=oneshot
ExecStart=PATH/backup.sh

[Install]
WantedBy=default.target

Then I set permissions for the script:

chmod 744 PATH/backup.sh

And installed the service:

chmod 664 /etc/systemd/system/backup.service
systemctl daemon-reload
systemctl enable backup.service

When I reboot the script fails:

● backup.service - backup script
   Loaded: loaded (/etc/systemd/system/backup.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2017-05-13 13:39:54 -03; 47min ago
 Main PID: 591 (code=exited, status=1/FAILURE)

Result of journalctl -xe:

mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Main process exited, code=exited, status=6/NOTCONFIGURED
mai 16 23:34:27 rodrigo-acer systemd[1]: Failed to start backup script.
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Unit entered failed state.
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Failed with result 'exit-code'.

What could be wrong?

8
  • 1
    The systemd journal should have recorded all of its output. Try journalctl -u backup
    – Zan Lynx
    May 13, 2017 at 17:38
  • 1
    And remember you don't have to reboot to try running a systemd job. Get it working with systemctl start backup first.
    – Zan Lynx
    May 13, 2017 at 17:40
  • Apart from a couple of SPECIFIERS (find that section in man systemd.unit), systemd does not perform environment variable substitution in unit files.
    – grochmal
    May 13, 2017 at 17:40
  • Other things I would do: set PATH explicitly in your script, or use absolute paths for all commands.
    – Zan Lynx
    May 13, 2017 at 17:41
  • @ZanLynx, I read the OP is indicating that they just replaced that for the question as an anonymization effort ("replaced password, user, and paths above"), and that it's really a legit absolute path in the genuine code. May 13, 2017 at 17:43

2 Answers 2

4

Solved guys. There was 2 problems:

1 - I had to change the service unit file to make the service run only after network was up. The unit section was changed to:

 [Unit]
 Description = World server backup
 Wants = network-online.target
 After = network.target network-online.target

2 - The root user did not have the remote host added to the known host list, unlike the ordinary user I used to test the script.

2

Failed with result 'exit-code' you could try this on your last line:

# REQUIRED FOR SYSTEMD: 0 means clean no error
exit 0

You may also need to add:

Type=forking

to the systemd entry similar to: https://serverfault.com/questions/751030/systemd-ignores-return-code-while-starting-service

If your service or script does not fork add a & at the end to run it in the background, and exit with 0 fast. Otherwise it will be like a startup that times out and takes forever / seems like frozen service.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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