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

hal_sk

New Member
Joined
May 25, 2020
Messages
14
Reaction score
3
Credits
148
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?
 


dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
329
Credits
3,199
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.
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,849
Reaction score
2,603
Credits
27,830
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:
 

dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
329
Credits
3,199
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.
 
OP
hal_sk

hal_sk

New Member
Joined
May 25, 2020
Messages
14
Reaction score
3
Credits
148
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
 

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
1,003
Reaction score
683
Credits
5,031
Doesn't echo require an argument?

/usr/bin/echo "something".
 

dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
329
Credits
3,199
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
 

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
1,003
Reaction score
683
Credits
5,031
I am a little curious why echo is needed at all?

date > /path/to/file
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,849
Reaction score
2,603
Credits
27,830
I tried doing the same as you.
Code:
[[email protected] ~]# cat /etc/systemd/system/mytest.service
[Unit]
Description=Test service

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

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

ssh [email protected]
[email protected]'s password:
Last login: Wed Sep 15 18:59:42 2021 from 11.22.13.1
[[email protected] ~]# 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:

Lord Boltar

Well-Known Member
Joined
Nov 24, 2020
Messages
968
Reaction score
625
Credits
7,187
[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
 
OP
hal_sk

hal_sk

New Member
Joined
May 25, 2020
Messages
14
Reaction score
3
Credits
148
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.
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,849
Reaction score
2,603
Credits
27,830
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:
[email protected]:~# lsb_release -a
Distributor ID:    Linuxmint
Description:    Linux Mint 20.2
Release:    20.2
Codename:    uma
[email protected]:~# cat /etc/systemd/system/mytest.service
[Unit]
Description=Test service

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

[Install]
WantedBy=multi-user.target

[email protected]:~# cat /usr/local/bin/test.sh
#!/bin/bash
date >> /home/tux/test.log
[email protected]:~# systemctl enable mytest
[email protected]:~# reboot

ssh [email protected]
[email protected]'s password:
Last login: Wed Sep 15 19:19:21 2021 from 11.22.13.1
[email protected]:~$ 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:
OP
hal_sk

hal_sk

New Member
Joined
May 25, 2020
Messages
14
Reaction score
3
Credits
148
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.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Staff online


Top