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).
To execute a terminal or any commands even another bash script within a BASH SCRIPT, all you have to do is simply start with a dollar sign and enclose the whole commands and arguments if any with PARENTHESES as follows.
In his case, it would be
The statement above will EXECUTE the SHUTDOWN command for 10 minutes system shutdown and spit out the actual date and time the system will be automatically shutdown just like you would run this command in a console. There is no need to turn the user into sudoer or superuser. Whenever he runs his bash script as a ROOT USER or using SUDO, he will be PROMPTED to enter the root password. That's all he has to do and the above command will be executed.
Plus, if there is ever a need to capture the output of any command or script, do as follow.