4

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).

  • can you start 'sudo /sbin/shutdown -h +10' from commandline? – UtLox Mar 15 '19 at 15:08
  • 1
    Why not use cronjobs? – Shardj Mar 15 '19 at 15:10
  • https://mxlinux.org/forum/viewtopic.php?t=48026 suggests `systemctl poweroff -i` or `dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.Poweroff" boolean:true` – jhnc Mar 15 '19 at 16:26
  • @UtLox, if I start this from command line, it works just fine **if** I provide the sudo password. I want this script automatically executed by the system, without me typing the password. But if I make this script executable as root, I can't get the notifications. So far I have to decide... executed by user -> can't shutdown... executed by root -> can't send notifications. – Vinícius Valente Mar 15 '19 at 17:32
  • @GeorgeAppleton, as far I know, cronjobs is used to execute tasks at specific times, is this right? I want execute this script to perform a process check every hour after turn on the computer. I think the problem with cronjob is, if I set the script to be executed from the crontab every hour (12h00, 13h00, 14h00 ...) and I turn on the computer at 12h59 it will shutdown just 1 minute later (if the specific process weren't running). – Vinícius Valente Mar 15 '19 at 17:41
  • @jhnc thanks for the comment. I don't know about the `systemctl poweroff`but I will learn more about it... but in this forum the people are complainning about the system not shutting down. My OS shuts down just fine, I just can't get it to be done automatically by this script. – Vinícius Valente Mar 15 '19 at 18:00

2 Answers2

1

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.

$(COMMANDS)

In his case, it would be

$(sudo shutdown 10)

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.

my_shuttime=$(sudo shutdown 10)
ThN
  • 3,235
  • 3
  • 57
  • 115
0

I think it lacks an entry for shutdown in the sudoers. Please create a file sudo under /etc/sudoers.d and make the following entry:

[YOURUSER] ALL = (ALL) NOPASSWD: /sbin/shutdown

Replace [YOURUSER] with your user account!

UtLox
  • 3,744
  • 2
  • 10
  • 13
  • Is this the same as adding via **sudo visudo**? If yes, I've already tried add this: `my_user ALL=NOPASSWD: /sbin/shutdown` Result: I get the notification just fine, but the system don't shutdown. – Vinícius Valente Mar 15 '19 at 17:50
  • (ALL) is missing – UtLox Mar 15 '19 at 17:52
  • I changed the script to `sudo /sbin/shutdown -h +1` to shutdown quickly, made your suggestion via `sudo visudo` and rebooted... Only got the notification. But if I add a file under /etc/sudores.d with the content you mentioned IT WORKS! Thank you! – Vinícius Valente Mar 15 '19 at 18:27
  • So why the hell it doesn't work when adding via `sudo visudo`? – Vinícius Valente Mar 15 '19 at 18:28
  • The original poster problem or issue has NOTHING to do with enabling an user to a super user. What he wants to be able to do is EXECUTE a TERMINAL COMMAND within a bash script. Whenever he runs the bash script, he needs to run it with sudo command and enter the password for root when prompted. That should solve his issue. He just wanted to know how to execute SHUTDOWN COMMAND within the script. NOTHING MORE NOTHING LESS. – ThN May 13 '22 at 06:10