Systemd service works when started, but not when enabled

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question


















  • 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






  • 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














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.







share|improve this question


















  • 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






  • 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












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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








edited Apr 3 at 7:48









Kevdog777

2,057113257




2,057113257










asked Apr 3 at 7:30









Subzero123

1




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






  • 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




    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




    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















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)