SystemD service does not execute a .sh script as intended

TLDR: Created service does not execute a .sh script as intended.

Hi all,

I am fairly new to this forum so please excuse me if this posted in the wrong section of the forum.

I want to create a service for my Ubuntu Server 19.04, I want to run a bash script when the service starts. Simple script pure for testing. The document and .sh file are in the same directory and the script works as intended.

The service can run and will show as active with systemctl status. However the script will not be executed. Hello will not be appended to the file nor will it be created. Does anyone know where I go wrong, As far as I know the service file itself does not need to be executable.

Thanks in advance!

The service is created in /etc/systemd/system

test.service

``
[Service]
ExecStart = /home/Bamidrone/Documents/test.sh

``
Bash script

``
#!/bin/sh

echo “hello” >> document

``

1 Like

Ubuntu usually puts the user’s name as their home directory, is the script in /home/Bamidrone/Documents/test.sh instead of /home/Documents/test.sh?

Yes, I made a typo, tnx for pointing out my error.

1 Like

Have you tried running the script outside of Systemd?

I am pretty sure that systemd runs services as root unless specified otherwise in the service file. So either manually specify your user as the user to run the service under, or make sure root has permissions to run the script correctly.

1 Like

the full path of “document” must be supplied. EX:
echo “hello” >> /home/Bamidrone/Documents/

what you have will probably write the file in /

edit: you may need to specify the full path to echo as well eg: /usr/bin/echo/ “hello”

I have run the script without systemD and it workd properly, I used chmod 777 on the sh file to make sure they can run it. Tnx for your reply

The script works fine, the file will append hello to document, which is created in the same directory.

as far as I can see something funky goes wrong with systemD, but I don’t see where :frowning:

have you checked journalctl for errors?

Yes, sadly no errors. I used this video as reference btw: https://www.youtube.com/watch?v=fYQBvjYQ63U&t=101s

so for clarification:
the script works fine when run manually eg ./test
the script does not work properly when run via systemd (and no systemd errors in journalctl) .
systemd says the script is running fine

note: i believe that systemd has no $PATH variable; i believe it runs as root (similar to cron, which also has no $PATH) . thats why i said you need to specify full paths.
it will eliminate that possibility.

2 Likes

Yes, the script runs without systemd, it does not work when configured in systemd.

I will try using full paths and tell you if it works.

Thank you for your response.

You were completely right, I needed to specify the full path.

Kind regards!

2 Likes