1

I'm writing a linux service test-thingy as a precursor to a project I am developing. I am trying to understand how it works, and why its failing right now.

My current goal is to create a linux (Ubuntu specifically in my case) service that monitors a text file, check.txt, for changes. The current contents of this file is simply the word "yes" on the first line (no quotes though). I want my service to change the word "yes" to "no", and change it back to "no" if I go in and change it to "yes" at any point in time. This is what I have so far.


My service file: trial.service

[Service]
ExecStart=/home/jeremiahdgage/trial.sh
User=root
Group=root

I know using root here isn't the best practice, but it will be necessary later on, so I am leaving it for now.


My bash file: trial.sh

#!/bin/bash
while true
do
        line=$(head -n 1 filename)

        if [ "$line" == "yes" ]
                then
                sudo sed -i 's/yes/no/g' /home/jeremiahdgage/check.txt
        fi

        echo changes made at $(date)
        sleep 1
done

while true means that it goes forever, line is the first line of my document. I check the first line to see if its "yes" then I use sed to swap it for "no". I close my if, echo the time and sleep for 1 ms before repeating the process again.

However, something is wrong because my service fails to start... If I check my status with systemctl status trial.service, it says there is a "permission denied" error at Exec.

What did I do wrong?


Update:

Service now starts but bash code does not work. 

  • Post the full output of the systemctl status command, that's pretty relevant here. – filbranden Mar 2 at 5:11
  • 1
    @filbranden Sure thing... here it goes. – ragnvaldr.js Mar 2 at 5:14
  • Maybe it's missing the execute bit? What does ls -l /home/jeremiahdgage/trial.sh say? You can try chmod +x /home/jeremiahdgage/trial.sh to see if that fixes it... How about ls -ld /home/jeremiahdgage/? Is that home directory on a remote filesystem such as NFS or something similar? Or maybe encrypted home directory with ecryptfs? Anything else peculiar about it? – filbranden Mar 2 at 5:21
  • 1
    Fudge. Yeah that was it... I added the bit when I started, but I must have just typed it in wrong. Yeah the service starts now but the script doesnt work still. – ragnvaldr.js Mar 2 at 5:24
  • 1
    No, I know what I did wrong there. Stupid copy/paste thing. Thanks... your a boss. – ragnvaldr.js Mar 2 at 5:27
1

It looks like you're missing execute permissions in your script.

You can fix that with:

$ chmod +x trial.sh

That's one case where even running as root won't work, root can otherwise bypass permissions but the file itself needs to be executable to be run.

(Running as root means permissions of the home directory itself wouldn't matter in most cases, even though in some cases such as NFS mounted home directory or perhaps a security module such as AppArmor or SELinux could prevent root from accessing the home directory.)

One good first step to troubleshoot this issue is to actually try to run this script directly, not under systemd, from a root shell. If you did so, you would notice it would fail for the same reason:

# cd /home/jeremiahdgage/
# ./trial.sh
bash: ./trial.sh: Permission denied

This can tell you that running it as a service is not really the issue, since you also reproduce it from the shell too.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.