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: Starting "Testing"... Aug 20 14:21:49 ip-10-110-103-202 sudo: No status data could be sent: $NOTIFY_SOCKET was not set Aug 20 14:21:49 ip-10-110-103-202 systemd: test.service: Main process exited, code=exited, status=1/FAILURE
[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
!/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.