<SOLVED> 'at' to kill pids and shut down

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
edcompsci
Level 2
Level 2
Posts: 69
Joined: Fri Dec 21, 2012 5:25 pm

<SOLVED> 'at' to kill pids and shut down

Post by edcompsci »

I am trying to make a simple bash script that will kill my browsers then shut down at a specific time using the 'at' command.

I have incomplete .sh file and a .txt files like follows:

Code: Select all

#!/bin/bash

clear
echo "Please enter a time to shut off applications and shut down system."
echo "use format HH:MM"

read Time

echo "Thank you."

echo "Now running the at command to shut down browsers and then shut down the system."

while read -r line; do   ; done < input-to-at-command.txt
 | at $Time

Code: Select all

killall firefox
killall chrome
I think I have the general idea but any help on finishing this code is appreciated


Or does someone have a better idea for doing this?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
lmuserx4849

Re: 'at' to kill pids and shut down

Post by lmuserx4849 »

Keep it real simple :-) and check out the shutdown command.

You can specify a time and message. No need to kill individual apps, unless there are special requirements.

Doc and examples of using at, like:

Code: Select all

at -m 01:35 < my-at-jobs.txt
In the my-at-jobs.txt, you could include commands.
Habitual

Re: 'at' to kill pids and shut down

Post by Habitual »

Why kill the browsers if you are shutting down?
lmuserx4849

Re: 'at' to kill pids and shut down

Post by lmuserx4849 »

If you want to beef up your bash script skills, linuxcommand.org, has a really good tutorial. imho, it's the best source. His book can be downloaded at: free.

Of course there is the bash man page and reference. You can view it via man or info, but it is also usually in html form in a distro's, /usr/share/doc/bash. Then there is the unofficial, but awesome bash faq. You'll find greg regularly on the bash email list. There are also guides at the Linux Documentation Project (I'm not sure of the status of tldp in general).

If I were writing a syllabus for school, those would be the resources I'd use :-)
User avatar
all41
Level 19
Level 19
Posts: 9473
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: 'at' to kill pids and shut down

Post by all41 »

or even just suspend:

Code: Select all

dbus-send --system --print-reply \
    --dest="org.freedesktop.UPower" \
    /org/freedesktop/UPower \
    org.freedesktop.UPower.Suspend & exit
Most of my systems use <4 watts in suspend mode and thaw within 5 seconds
Everything in life was difficult before it became easy.
edcompsci
Level 2
Level 2
Posts: 69
Joined: Fri Dec 21, 2012 5:25 pm

Re: 'at' to kill pids and shut down

Post by edcompsci »

lmuserx4849 wrote:Keep it real simple :-) and check out the shutdown command.

You can specify a time and message. No need to kill individual apps, unless there are special requirements.

Doc and examples of using at, like:

Code: Select all

at -m 01:35 < my-at-jobs.txt
In the my-at-jobs.txt, you could include commands.

Thanks for this. I thought maybe doing killall would assure that nothing hangs, should I want it to shutdown after I leave. I like the redirect statement that you shared.
ColdBoot

Re: <SOLVED> 'at' to kill pids and shut down

Post by ColdBoot »

Habitual wrote:Why kill the browsers if you are shutting down?
Because if you don't close them properly, eventually you will end up with a corrupt browser next time with all inexplicable weirdness which that involves.

@OP
I would suggest a different approach. Instead of killing browsers, which is essentially what shutdown process does, It is better to close their windows using wmctl because it is the same thing as pressing Alt+F4 or clicking on the X button.

Here is what I use as a replacement for shutdown button:

Code: Select all

#! /bin/bash

close_apps () {
	WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$" | cut -f1 -d' ')
	for i in $WIN_IDs; do wmctrl -ic "$i"; done

	# Keep checking and waiting until all windows are closed
	while [ "$WIN_IDs" != "" ]; do
        	sleep 0.1;
        	WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$" | cut -f1 -d' ')
	done
}
close_apps
# Waiting for apps to write what they need prior to their full exit
sleep 3
systemctl poweroff
One more benefit is that in case I have forgotten to save changes in a document open on one of the desktops, it will not shutdown but wait till I do so. In XFCE, in addition to Desktop window, you should not attempt to close the panel because it invokes immediate logout and the script stops(this script is set for Cinnamon). Also, the name of the Desktop varies depending on language used so run

Code: Select all

wmcltl -l
to see what are the names of all running system windows which you should not attempt to close in the close_apps procedure.
edcompsci
Level 2
Level 2
Posts: 69
Joined: Fri Dec 21, 2012 5:25 pm

Re: <SOLVED> 'at' to kill pids and shut down

Post by edcompsci »

Sorry for not replying sooner This looks like a really cool script I will want to try as soon as I get a chance. Thanks.
Locked

Return to “Scripts & Bash”