[Solved] Script does not run after reboot (systemd)

hal_sk

New Member
Joined
May 25, 2020
Messages
19
Reaction score
3
Credits
188
Hello,
I have simple script /usr/local/bin/test.sh
Code:
#!/bin/bash
echo $(date) >> /home/hal/Desktop/log

Then I have this service file: /etc/systemd/system/test.service
Code:
[Unit]
Description=Test service

[Service]
ExecStart=/usr/local/bin/test.sh

[Install]
WantedBy=multi-user.target

Then I run commands:
sudo chmod +x /usr/local/bin/test.sh
...and...
sudo systemctl daemon-reload


After command:
sudo systemctl start test.service
...script will run correctly.

After command:
sudo systemctl enable test.service
...some symlink is created:
Created symlink /etc/systemd/system/multi-user.target.wants/test.service → /etc/systemd/system/test.service.

But after reboot script will not run
Status systemctl status contains:
Code:
/usr/local/bin/test.sh: line 2: /home/hal/Desktop/log: No such file or directory
and
Code:
"Main process exited, code=exited, status=203/EXEC"
What's wrong with my setup pls?
 


Try adding /usr/bin/echo to the script. (ie, add the path to the echo command)

I suspect it doesn't know where the echo command is because it doesn't by default have a $PATH variable set.

Might do the same for the date command.
 
Also under Service try adding this.
Code:
Type=oneshot
Since the script you are running is not started as a daemon but something that is executed once and then it exists:
 
Also under Service try adding this.
Code:
Type=oneshot
Since the script you are running is not started as a daemon but something that is executed once and then it exists:
Good call, you don't want systemd attempting to restart it over and over because it keeps exiting.
 
Well I have just tried to add "Type=oneshot" into service like this:
Code:
[Unit]
Description=Test service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/test.sh

[Install]
WantedBy=multi-user.target
But did not work.

And also I tried to add "/usr/bin/echo" as dcbrown73 mentioned but I don't understand why and where to put it. I tried this bellow but not working.
Code:
[Unit]
Description=Test service

[Service]
Type=oneshot
ExecStart=/usr/bin/echo /usr/local/bin/test.sh

[Install]
WantedBy=multi-user.target
 
Doesn't echo require an argument?

/usr/bin/echo "something".
 
Doesn't echo require an argument?

/usr/bin/echo "something".
Sure. His script is echoing the date into a file.

Code:
#!/bin/bash
echo $(date) >> /home/hal/Desktop/log

Though I was suggesting updating it with paths.
Code:
#!/bin/bash
/usr/bin/echo $(/usr/bin/date) >> /home/hal/Desktop/log
 
I am a little curious why echo is needed at all?

date > /path/to/file
 
I tried doing the same as you.
Code:
[root@rhel8 ~]# cat /etc/systemd/system/mytest.service
[Unit]
Description=Test service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/test.sh

[Install]
WantedBy=multi-user.target
[root@rhel8 ~]# cat /usr/local/bin/test.sh
#!/bin/bash
date >> /home/tux/test.log
[root@rhel8 ~]# systemctl enable mytest
[root@rhel8 ~]# reboot
Connection to rhel8 closed by remote host.
Connection to rhel8 closed.

ssh root@rhel8
root@rhel8's password:
Last login: Wed Sep 15 18:59:42 2021 from 11.22.13.1
[root@rhel8 ~]# cat ~tux/test.log
Wed Sep 15 19:01:21 CEST 2021
As you can see it works, so my guess is you are forgetting to share something or the path /home/hal/Desktop doesn't exist? Can you share the output of the following?
Code:
ls -l /home/hal/Desktop
 
Last edited:
[Unit]
Description=Test Service
After=network.target
[Service]
Type=simple
ExecStart=/bin/bash /usr/local/bin/test.sh
[Install]
WantedBy=multi-user.target

Reload all systemd service files: systemctl daemon-reload

Check that it is working by starting the service with systemctl start test

Next, we create our custom shell script to be executed during systemd startup.

#!/bin/bash

date > /root/test_report.txt
du -sh /home/ >> /root/test_report.txt

Before we reboot our system we need to make our script executable:
# chmod 744 /usr/local/bin/test.sh
Next, install systemd service unit and enable it so it will be executed at the boot time:

# chmod 664 /etc/systemd/system/test.service
# systemctl daemon-reload
# systemctl enable test.service
Created symlink from /etc/systemd/system/default.target.wants/test.service to /etc/systemd/system/test.service.

If you wish to test your script before you reboot run:

# systemctl start test.service
# cat /root/test.txt
 
As you can see it works, so my guess is you are forgetting to share an important detail. Does this path even exist: /home/hal/Desktop? Can you share the output of the following?
For full enclosure I have PC with 2 Linux Mints (boot selection). One of the Mint is running script fine after reboot and other Mint is not. Those paths are correct. Just the user name "hal" in paths is changed based on used OS. In next day or two I will try to test it again (for each OS) with even simpler example with exactly same paths and scripts. I will be post it here. Thanks so far.
 
For full enclosure I have PC with 2 Linux Mints (boot selection). One of the Mint is running script fine after reboot and other Mint is not. Those paths are correct. Just the user name "hal" in paths is changed based on used OS. In next day or two I will try to test it again (for each OS) with even simpler example with exactly same paths and scripts. I will be post it here. Thanks so far.

Code:
root@mint:~# lsb_release -a
Distributor ID:    Linuxmint
Description:    Linux Mint 20.2
Release:    20.2
Codename:    uma
root@mint:~# cat /etc/systemd/system/mytest.service
[Unit]
Description=Test service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/test.sh

[Install]
WantedBy=multi-user.target

root@mint:~# cat /usr/local/bin/test.sh
#!/bin/bash
date >> /home/tux/test.log
root@mint:~# systemctl enable mytest
root@mint:~# reboot

ssh tux@mint
tux@mint's password:
Last login: Wed Sep 15 19:19:21 2021 from 11.22.13.1
tux@mint:~$ cat test.log
Wed 15 Sep 2021 07:21:51 PM CEST
Which Mint version is working and which isn't?
But after reboot script will not run
Status systemctl status contains:
Code:
/usr/local/bin/test.sh: line 2: /home/hal/Desktop/log: No such file or directory
and
Code:
"Main process exited, code=exited, status=203/EXEC"
What's wrong with my setup pls?
According to that error the path doesn't exist so it can't create the log file.
 
Last edited:
It is working now.
The problem was most likely "echo $(date) >>" in the script. I changed it to just "date >>" and it works now in both Mints on reboot and without errors. It was my mistake I have not much skills in shell.
Thx to all.
 

Members online


Latest posts

Top