I have a bash script that checks the status of caps lock and num lock and then notifies the user whenever it's changed, if I run it from the terminal everything works fine but if I initiate it from systemd, the "systemctl status command" gives me the following error:

xset: unable to open display "" 

here is the source code:

#!/bin/bash source /usr/lib/kbled-notifier/delay.conf echo "kbled-notifier.service: ## Starting ##" | systemd-cat -p info #Validate the $delay parameter case $delay in ''|*[!0-9]*) delay=2000 ;; *) ;; esac nofity-it () { gdbus call --session \ --dest=org.freedesktop.Notifications \ --object-path=/org/freedesktop/Notifications \ --method=org.freedesktop.Notifications.Notify \ '' $IDSET "$icon" "$title" "$text" \ '[]' '{"urgency": <1>}' $delay } parse-id () { sed 's/(uint32 \([0-9]\+\),)/\1/g' } IDSET=0 wasoffc="true" wasoffn="true" while [ true ] do #Retrieves the current status of Caps Lock and Num Lock capstat=$( xset -q | sed -n 's/^.*Caps Lock:\s*\(\S*\).*$/\1/p' ) numstat=$( xset -q | sed -n 's/^.*Num Lock:\s*\(\S*\).*$/\1/p' ) if [[ $capstat == "on" ]] && [ ${wasoffc} = "true" ]; then title="Caps Lock" text="ON" icon="/usr/share/icons/kbled-indicator/capson.png" IDSET=$(nofity-it | parse-id) wasoffc="false" fi if [[ ${wasoffc} == "false" ]] && [[ $capstat == "off" ]]; then title="Caps Lock" text="OFF" icon="/usr/share/icons/kbled-indicator/capsoff.png" IDSET=$(nofity-it | parse-id) wasoffc="true" fi if [[ $numstat == "on" ]] && [[ ${wasoffn} == "true" ]]; then title="Num Lock" text="ON" icon="/usr/share/icons/kbled-indicator/numon.png" IDSET=$(nofity-it | parse-id) wasoffn="false" fi if [[ $numstat == "off" ]] && [[ ${wasoffn} == "false" ]]; then title="Num Lock" text="OFF" icon="/usr/share/icons/kbled-indicator/numoff.png" IDSET=$(nofity-it | parse-id) wasoffn="true" fi sleep 0.05 done 

this is the content of the service:

[Unit] Description=A Notifier For Status of Caps Lock & Num Lock [Service] Type=simple ExecStart=/usr/lib/kbled-notifier/kbled-notifier-daemon Restart=on-failure RestartSec=10 KillMode=process [Install] WantedBy=multi-user.target 
7

Related questions 6195 How do I get the directory where a Bash script is located from within the script itself? 4409 How do I check if a directory exists or not in a Bash shell script? 3083 How can I check if a program exists from a Bash script? Related questions 6195 How do I get the directory where a Bash script is located from within the script itself? 4409 How do I check if a directory exists or not in a Bash shell script? 3083 How can I check if a program exists from a Bash script? 3954 How do I tell if a file does not exist in Bash? 3516 How to concatenate string variables in Bash 2796 Extract filename and extension in Bash 3495 How to check if a string contains a substring in Bash 1952 Check existence of input argument in a Bash shell script 2878 How do I split a string on a delimiter in Bash? 2070 Looping through the content of a file in Bash Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.