1

I am creating a systemd service that needs to run a bash script with sudo privs. I noticed that when I run it, it hangs forever in "status=Activating".

To try to replicate it, I setup a toy example. I have a systemd unit that will run a very simple long-running bash script that does a systemd_notify right away and then loops forever. I run the script with sudo. However, the service fails to start and I get this error:

Aug 20 14:21:48 ip-10-110-103-202 systemd[1]: Starting "Testing"...
Aug 20 14:21:49 ip-10-110-103-202 sudo[24772]: No status data could be sent: $NOTIFY_SOCKET was not set
Aug 20 14:21:49 ip-10-110-103-202 systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE

test.service:

[Unit]
Description="Testing"
Requires=network-online.target
After=network-online.target

[Service]
Type=notify
User=consul
Group=consul
ExecStart=/usr/bin/sudo /home/psengupta/long_test.sh

[Install]
WantedBy=multi-user.target

long_test.sh:

!/usr/bin/env bash
set -euo pipefail

systemd-notify --ready --status="Started"
while true
do
        echo "Press CTRL+C to stop the script execution"
        # Enter your desired command in this block.
done

When I remove the sudo in front of the command, it works fine! e.g ExecStart=/home/psengupta/long_test.sh

Why does using the sudo before running the long_test.sh script actually fail my service? I am not very familiar with this stuff and not sure why $NOTIFY_SOCKET is not set with the sudo command usage. Ideally, I would like to set it and run the script with sudo and notify when it is started.

0

Read man sudo sudoers and use sudo -E to pass your whole set of environment variables along.

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.