Script to shutdown pc

Ubuntu 24.04, new install
Gnome, X-11
I have a script that closes all open apps gracefully, I’d like to add at the end of script command to shutdown my pc, what’s the alternative to shutdown command (which requires root password), that can shutdown/poweroff the pc without asking for root password?

You could add the shutdown command to your sudo configuration, so it’s allowed without entering a password, for your account.

Here’s an answer copy/pasted from AU.


Open up a terminal (CTRL + T)
and type the following sudo visudo

Add the following line:

%group_name ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown

or

user_name ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown

This allows the user/group to run the above three commands, using sudo, however with no password.

So, sudo poweroff will now result in a passwordless shutdown.


However, to make this even cleaner, we’ll add an alias, so that running shutdown calls sudo shutdown now.

Open ~/.bash_aliases for editing.

nano ~/.bash_aliases

Insert the following line at the end of the file:

alias shutdown='sudo shutdown now' 

Finally, load the changes to the .bash_aliases file…

source ~/.bash_aliases

Try it out!

shutdown
1 Like

Actually, since systemd was introduced, you no longer need sudo to shut down the pc, you just need the command

systemctl poweroff
3 Likes

Instead of hacking up sudo or something, you can also use dbus:

dbus-send --system --print-reply \
  --dest=org.freedesktop.login1 /org/freedesktop/login1 \ 
  "org.freedesktop.login1.Manager.PowerOff" boolean:true

I tried with my script and it did not work, I think it has something to do with my script (I did not write it myself, I got it from some post I saw awhile ago, and I don’t fully understand it). It seems this script stopped working after closing all apps, which change I have to do in order to continue the shutdown process? This is the script I have:

#!/bin/bash

while read line
do
    wmctrl -i -c "$line"
done< <(wmctrl -l | awk '{print $1}')
#sleep 2
systemctl poweroff
#dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true

Try just using the command I gave you…

ps: don’t need a script , just an alias

1 Like

I’m trying to perform 2 tasks:

  1. close gracefully all open apps
  2. power off the pc
    to shut down all apps, I need script, and I’d like to include the command to poweroff the pc after all apps are closed.

If you install Actiona (Qt objects) from Ubuntu repo you can run scripts such as (to begin) very simple poweroff below. You can add other commands such as preceding “poweroff command” with your own custom graceful shutdown script you have working. Then to run this “alias” run the command:

actexec /path/to/Shutdown.ascr

Using such thin automation layer you can run any combination of commands, some very complex. Indeed I have an AI Agent crafting such scripts for me (from my directions) as I navigate the Ubuntu desktop and cloud interfaces.

file:///path/to/Shutdown.ascr

<?xml version="1.0" encoding="UTF-8"?>
<scriptfile>
    <settings program="actiona" version="3.10.1" scriptVersion="1.1.0" os="GNU/Linux"/>
    <actions>
        <action name="ActionCommand" version="1.0.0"/>
    </actions>
    <parameters/>
    <resources/>
    <script pauseBefore="0" pauseAfter="0">
        <action name="ActionCommand" comment="systemctl poweroff">
            <exception id="0" action="0" line=""/>
            <exception id="1" action="0" line=""/>
            <exception id="2" action="1" line=""/>
            <exception id="32" action="0" line=""/>
            <parameter name="command">
                <subParameter name="value" code="0">systemctl</subParameter>
            </parameter>
            <parameter name="errorOutput">
                <subParameter name="value" code="0"></subParameter>
            </parameter>
            <parameter name="exitCode">
                <subParameter name="value" code="0"></subParameter>
            </parameter>
            <parameter name="exitStatus">
                <subParameter name="value" code="0"></subParameter>
            </parameter>
            <parameter name="output">
                <subParameter name="value" code="0"></subParameter>
            </parameter>
            <parameter name="parameters">
                <subParameter name="value" code="0">poweroff</subParameter>
            </parameter>
            <parameter name="processId">
                <subParameter name="value" code="0"></subParameter>
            </parameter>
            <parameter name="workingDirectory">
                <subParameter name="value" code="0"></subParameter>
            </parameter>
        </action>
    </script>
</scriptfile>

Copy this code into your desktop with mime type *.ascr.

Then run

actexec Shutdown.ascr

or within the Actiona GUI

and this will shut down your PC … abruptly. So you need your preceding script to prepare for abrupt shutdown. Be warned.

Experiment with Actiona first. You might add a music jingle as you Shutdown.

Thanks for this tips, I’ll take a look.

Thanks for this tip, it seems a very interesting tool, where can I find tutorial/manual to study/learn it better, I’ve tried to run your script, but it did not work, the command is actexec Shutdown.ascr or actiona -s Shutdown.ascr. I’ll try to test it further, but couldn’t find additional material to learn.

I’m trying to create a scraper to get product details for a website, but so far unsuccessful, it seems to me that I could use this tool to do it.

Actiona admittedly is an old tool dating back to games automation. You have to dig through old forum discussions to learn.

Further links here.

http://actiona.tools/

http://jmgr.net/forum/

https://wiki.actiona.tools/doku.php?id=en:scripts

https://wiki.actiona.tools/doku.php?id=en:actions

Accessing the old forum http://jmgr.net/forum/ there is a French forum and English forum.

The author created this link “Multiple tools can be used in combination with Actiona to extend its features”. Unfortunately there are no examples but use your own intuition. You can drive an tool … including Albert which is where we came in.

I did not realise until looking just now that my old contribution re: ChatGPT would garner so many hits.

https://forum.jmgr.net/viewtopic.php?t=19390

But I have proven that concept by recently getting an AI Agent to “drive” Actiona. “An AI Agent driving Ubuntu desktop?”. That might be a bit giddy for some. Lookout for this thread to be flagged as OT - again.

Note that in learning the ropes I dived into French forum since creator is French. I just clicked on “translate into English”.

My advice is to start with the objects in the app. The Command object requires just the command then parameters takes the other data. There are multiple ways of running scripts. You just have to learn by practice.

Another tip. Use free XMLCopyEditor to view the innards of *.ascr scripts. But beginners shouls start learning the existing standard QT objects.

The opening subject of this discussion was simply “shutdown”. Could not be easier. But then later you might build scripts which dump data and so on.

On the subject of site scraping I would add a Python script exploiting BeautifulSoup. Follow the rules on scraping.

Your sites can be listed in an array in Code object.

I’ve tried, it did not work, the shutdown did not run (I think), it closed all open apps, then nothing. This is my shell script (I followed your instructions: 1) saved your code as ShutDown.ascr 2) changed my script to include your “alias”. I tested ShutDown.ascr, it worked in Actiona GUI, but not in my script.

#!/bin/bash

# Close all open windows gracefully
#echo "Closing open windows using wmctrl..."
wmctrl -l | while read -r line; do
    win_id=$(echo "$line" | awk '{print $1}')
    wmctrl -ic "$win_id"
done

# Wait for windows to close
sleep 2

# Optional: sync filesystem
sync

sleep 2

actexec /media/michael/data/backup/sync/script/ShutDown.ascr

Give me a bit of time to explore. Since you are the original poster (OP) I hope that you agree that this spinoff idea is not off topic (OT). It works here so I will investigate the difference and report later.

One point I note … your script you are placing in /media/ mounted folder. I suspect that you have permissions problems. I would not recommend using Actiona scripts in sudo mode unless you personally create your own scripts. Never accept external scripts from a third party (including me) requiring sudo mode. They could be bad actors. That principle of due diligence applies to any scripts in this forum. If permissions are needed Actiona can prompt the user for a password.

Try running ShutDown.ascr in a /Home folder such as /Home/Actions/

Check the permissions of each script therein. User permissions only not root.

If root mode is needed prompt the user for a password at that point. Trust no script.

Then your safe command might be
actexec /Home/Actions/ShutDown.ascr

I’m reading through this script, and I’m puzzled as to why it’s getting so complicated!

OP, does the command by @carlin0 not work for you? It works perfectly for me!

systemctl poweroff
1 Like

Paddy, we know that simple or complex scripts work in isolation. Learning CLI as we go along. The argument that is advanced is that a thin layer of automation can improve enormously the user productivity. This OP has a requirement to run a cascade of scripts (admittedly only two). But consider a workflow where we need to orchestrate a shutdown, including backups, and perhaps logging events. That is when UI automation (note X11 only not Wayland) steps forward. This seed OP requirement simple as it may be opens a Pandora’s box of ideas.

1 Like

Note further points. My sample script was named Shutdown.ascr.

But it has changed to
actexec /media/michael/data/backup/sync/script/ShutDown.ascr.

Spot the difference?

So there are three points I raise:
Check correct naming of script.
Check file path of script (not in sudo land)
Check permissions of script (user permissions only)

Instead of jumping at overcomplicated and rather obsolete tools, you should better research why your script stops execution after wmctl is done …

Try adding some debugging to it and find why you do not reach the systemctl shutdown command…

Try replacing the systemctl shutdown line with a simple echo "foo" > ~/shutdown-script.log and see if the file exists in your home dir after it ran… If not, there is something with your wmctl call that makes the script stop when wmctl is done…

Grabbing an automation tool to replace a known working and well proven command will not solve anything if you simply do not reach the point of execution in your script…

1 Like

Permissions. Filepath. File exact naming.

There are alternatives to old Actiona which is an example. But they rely upon X11 continuing and Wayland not blocking such UI orchestration. Standing on the shoulders of others.

1 Like

You just wrote “what do you mean?” (deleted) and I point to post 16 where I spot possible problems.