0

My systemd service is as follows.

/etc/systemd/system/caportal.service

[Unit]
Description=@CPACK_PACKAGE_DESCRIPTION_SUMMARY@

Before=network-pre.target
Wants=network-pre.target

DefaultDependencies=no
Requires=local-fs.target
After=local-fs.target

[Service]
Type=oneshot

ExecStart=/home/pi/test2.sh

RemainAfterExit=yes

[Install]
WantedBy=network.target

/home/pi/test2.sh

#!/bin/sh

echo Hi this working........ > output

It is made executable by sudo chmod 777 /home/pi/test2.sh. I have started the systemd service using sudo systemctl start caportal. It runs successfully.

output of sudo systemctl status caportal

● caportal.service - @CPACK_PACKAGE_DESCRIPTION_SUMMARY@
   Loaded: loaded (/etc/systemd/system/caportal.service; enabled)
   Active: active (exited) since Wed 2019-04-10 07:01:15 UTC; 1h 3min ago
  Process: 286 ExecStart=/home/pi/test2.sh (code=exited, status=0/SUCCESS)
 Main PID: 286 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/caportal.service

Apr 10 07:01:15 raspberrypi systemd[1]: Started @CPACK_PACKAGE_DESCRIPTION_SUMMARY@.

Its showing that the service is working fine but the file output is not written with "Hi this working........ ".

But when we run test2.sh using command `sudo /home/pi/test2.sh', its working fine.

Why the script is not working when used with systemd service?

1

Your script is writing to a relative path called output. A relative path does not begin with /; an absolute path begins with /.

The path is relative to the current working directory. Since you did not define WorkingDirectory= and you're running the service as a system service, it defaults to the root directory /. You should be able to find the file at /output (Current working directory / plus the relative path output).

If you are expecting the file to be written at a particular path, you should either specify an absolute path in your script or a WorkingDirectory= under [Service].

  • Yeah.. the output is present in root directory – mcv Apr 10 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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