Systemd service works when started, but not when enabled

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a bash script that detects when new directories are created under /var/log/myhosts/. When a new directory is created, it should create a directory named "myfolder" under it.
/etc/myscript.sh
#!/bin/bash
/usr/bin/inotifywait -m /var/log/myhosts -e create -e moved_to |
while read path action file; do
/usr/bin/mkdir -p "/var/log/myhosts/$file/myfolder"
done
I want to run this script from a systemd service unit on boot. I created and enabled the following systemd service unit:
/etc/systemd/system/myhosts.service
[Unit]
Description=Watches /var/log/myhosts/ for new directories
After=rsyslog.service
[Service]
Type=simple
ExecStart=/etc/myscript.sh
[Install]
WantedBy=multi-user.target
This script works as expected if I run it like:
bash /etc/myscript.sh
sh /etc/myscript.sh
cd /etc/ -> ./myscript.sh
systemctl start myhosts.service
crontab -e -> @reboot /etc/myscript.sh -> works after reboot
If I enable the systemd service unit and reboot my Linux system, after the reboot systemctl status myhosts.service says that the service is running, ps -ef | grep my returns that there is a process for the script and the script is running properly, but when I create directories in /var/log/myhosts/ it doesn't create a directory myfolder inside them.
I just want to know why the systemd service unit approach doesn't work after reboot. What am I doing wrong? As mentioned above, the crontab @reboot /etc/myscript.sh approach works as expected.
bash centos systemd
add a comment |Â
up vote
0
down vote
favorite
I have a bash script that detects when new directories are created under /var/log/myhosts/. When a new directory is created, it should create a directory named "myfolder" under it.
/etc/myscript.sh
#!/bin/bash
/usr/bin/inotifywait -m /var/log/myhosts -e create -e moved_to |
while read path action file; do
/usr/bin/mkdir -p "/var/log/myhosts/$file/myfolder"
done
I want to run this script from a systemd service unit on boot. I created and enabled the following systemd service unit:
/etc/systemd/system/myhosts.service
[Unit]
Description=Watches /var/log/myhosts/ for new directories
After=rsyslog.service
[Service]
Type=simple
ExecStart=/etc/myscript.sh
[Install]
WantedBy=multi-user.target
This script works as expected if I run it like:
bash /etc/myscript.sh
sh /etc/myscript.sh
cd /etc/ -> ./myscript.sh
systemctl start myhosts.service
crontab -e -> @reboot /etc/myscript.sh -> works after reboot
If I enable the systemd service unit and reboot my Linux system, after the reboot systemctl status myhosts.service says that the service is running, ps -ef | grep my returns that there is a process for the script and the script is running properly, but when I create directories in /var/log/myhosts/ it doesn't create a directory myfolder inside them.
I just want to know why the systemd service unit approach doesn't work after reboot. What am I doing wrong? As mentioned above, the crontab @reboot /etc/myscript.sh approach works as expected.
bash centos systemd
1
If/var/log/myhostswere on a different partition, and the service starts before that is mounted, behaviour like that wouldn't surprise me. (I.e. your script is looking at a location you can't reach in the filesystem anymore, because it's shadowed by a mount.)
â Ulrich Schwarz
Apr 3 at 9:06
1
How does cron work okay then? Hmm, maybe After=var.mount should be used because /var is on a different partition than /etc/.
â Subzero123
Apr 3 at 9:12
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a bash script that detects when new directories are created under /var/log/myhosts/. When a new directory is created, it should create a directory named "myfolder" under it.
/etc/myscript.sh
#!/bin/bash
/usr/bin/inotifywait -m /var/log/myhosts -e create -e moved_to |
while read path action file; do
/usr/bin/mkdir -p "/var/log/myhosts/$file/myfolder"
done
I want to run this script from a systemd service unit on boot. I created and enabled the following systemd service unit:
/etc/systemd/system/myhosts.service
[Unit]
Description=Watches /var/log/myhosts/ for new directories
After=rsyslog.service
[Service]
Type=simple
ExecStart=/etc/myscript.sh
[Install]
WantedBy=multi-user.target
This script works as expected if I run it like:
bash /etc/myscript.sh
sh /etc/myscript.sh
cd /etc/ -> ./myscript.sh
systemctl start myhosts.service
crontab -e -> @reboot /etc/myscript.sh -> works after reboot
If I enable the systemd service unit and reboot my Linux system, after the reboot systemctl status myhosts.service says that the service is running, ps -ef | grep my returns that there is a process for the script and the script is running properly, but when I create directories in /var/log/myhosts/ it doesn't create a directory myfolder inside them.
I just want to know why the systemd service unit approach doesn't work after reboot. What am I doing wrong? As mentioned above, the crontab @reboot /etc/myscript.sh approach works as expected.
bash centos systemd
I have a bash script that detects when new directories are created under /var/log/myhosts/. When a new directory is created, it should create a directory named "myfolder" under it.
/etc/myscript.sh
#!/bin/bash
/usr/bin/inotifywait -m /var/log/myhosts -e create -e moved_to |
while read path action file; do
/usr/bin/mkdir -p "/var/log/myhosts/$file/myfolder"
done
I want to run this script from a systemd service unit on boot. I created and enabled the following systemd service unit:
/etc/systemd/system/myhosts.service
[Unit]
Description=Watches /var/log/myhosts/ for new directories
After=rsyslog.service
[Service]
Type=simple
ExecStart=/etc/myscript.sh
[Install]
WantedBy=multi-user.target
This script works as expected if I run it like:
bash /etc/myscript.sh
sh /etc/myscript.sh
cd /etc/ -> ./myscript.sh
systemctl start myhosts.service
crontab -e -> @reboot /etc/myscript.sh -> works after reboot
If I enable the systemd service unit and reboot my Linux system, after the reboot systemctl status myhosts.service says that the service is running, ps -ef | grep my returns that there is a process for the script and the script is running properly, but when I create directories in /var/log/myhosts/ it doesn't create a directory myfolder inside them.
I just want to know why the systemd service unit approach doesn't work after reboot. What am I doing wrong? As mentioned above, the crontab @reboot /etc/myscript.sh approach works as expected.
bash centos systemd
edited Apr 3 at 7:48
Kevdog777
2,057113257
2,057113257
asked Apr 3 at 7:30
Subzero123
1
1
1
If/var/log/myhostswere on a different partition, and the service starts before that is mounted, behaviour like that wouldn't surprise me. (I.e. your script is looking at a location you can't reach in the filesystem anymore, because it's shadowed by a mount.)
â Ulrich Schwarz
Apr 3 at 9:06
1
How does cron work okay then? Hmm, maybe After=var.mount should be used because /var is on a different partition than /etc/.
â Subzero123
Apr 3 at 9:12
add a comment |Â
1
If/var/log/myhostswere on a different partition, and the service starts before that is mounted, behaviour like that wouldn't surprise me. (I.e. your script is looking at a location you can't reach in the filesystem anymore, because it's shadowed by a mount.)
â Ulrich Schwarz
Apr 3 at 9:06
1
How does cron work okay then? Hmm, maybe After=var.mount should be used because /var is on a different partition than /etc/.
â Subzero123
Apr 3 at 9:12
1
1
If
/var/log/myhosts were on a different partition, and the service starts before that is mounted, behaviour like that wouldn't surprise me. (I.e. your script is looking at a location you can't reach in the filesystem anymore, because it's shadowed by a mount.)â Ulrich Schwarz
Apr 3 at 9:06
If
/var/log/myhosts were on a different partition, and the service starts before that is mounted, behaviour like that wouldn't surprise me. (I.e. your script is looking at a location you can't reach in the filesystem anymore, because it's shadowed by a mount.)â Ulrich Schwarz
Apr 3 at 9:06
1
1
How does cron work okay then? Hmm, maybe After=var.mount should be used because /var is on a different partition than /etc/.
â Subzero123
Apr 3 at 9:12
How does cron work okay then? Hmm, maybe After=var.mount should be used because /var is on a different partition than /etc/.
â Subzero123
Apr 3 at 9:12
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f435220%2fsystemd-service-works-when-started-but-not-when-enabled%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
If
/var/log/myhostswere on a different partition, and the service starts before that is mounted, behaviour like that wouldn't surprise me. (I.e. your script is looking at a location you can't reach in the filesystem anymore, because it's shadowed by a mount.)â Ulrich Schwarz
Apr 3 at 9:06
1
How does cron work okay then? Hmm, maybe After=var.mount should be used because /var is on a different partition than /etc/.
â Subzero123
Apr 3 at 9:12