I'm trying to make an executable file (bash script) to show me a notification and shutdown my computer when a process is not found.
I will run the script as a Startup Application and I'm using the notify-send
and shutdown
commands in this script.
The problem is:
(1) If I add myfolder/myscript
to the Startup Applications list it can't run the shutdown
command (root password is required for this)
(2) If I add the script sudo myfolder/myscript
it can't show the notifications via notify-send
application.
I've already done a lot of searching around the internet and tried these steps:
(1) Added the script path or /sbin/shutdown
to the sudores via sudo visudo
(2) Added su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus
before notify-send
command (I found some users reporting that root can't send notifications)`
So... none of them worked. What I'm missing? What can be done to display notifications AND shutdown?
Here is my code:
#!/bin/bash
#Search for a specific process and sleep if it is found (removed for space saving)
shut_time=$(date --date='10 minutes' +"%T")
notify-send -t 600000 "WARNING:
Program is not running.
Shutting down in 10 minutes (scheduled for $shut_time)."
#ALREADY TESTED BELLOW LINES (DON'T WORK)
#su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus notify-send -t 600000 'WARNING:
#Program is not running.
#Shutting down in 10 minutes.'"
sudo /sbin/shutdown -h +10 #Tried with our without sudo
I'm running MX Linux 18 (xfce, Debian based).