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

Forum rules
Before you post please read how to get help
Post Reply
edcompsci
Level 1
Level 1
Posts: 34
Joined: Fri Dec 21, 2012 5:25 pm

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

Post by edcompsci » Mon Jul 18, 2016 10:02 pm

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 edcompsci on Sat Jan 14, 2017 1:09 am, edited 2 times in total.

lmuserx4849

Re: 'at' to kill pids and shut down

Post by lmuserx4849 » Tue Jul 19, 2016 2:05 am

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
Level 13
Level 13
Posts: 4870
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

Re: 'at' to kill pids and shut down

Post by Habitual » Tue Jul 19, 2016 7:01 am

Why kill the browsers if you are shutting down?

lmuserx4849

Re: 'at' to kill pids and shut down

Post by lmuserx4849 » Tue Jul 19, 2016 1:59 pm

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 13
Level 13
Posts: 4834
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: 'at' to kill pids and shut down

Post by all41 » Wed Jul 20, 2016 1:55 pm

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
Proud to be a supporter and monthly contributor to Mint.

edcompsci
Level 1
Level 1
Posts: 34
Joined: Fri Dec 21, 2012 5:25 pm

Re: 'at' to kill pids and shut down

Post by edcompsci » Sat Jan 14, 2017 1:07 am

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
Level 3
Level 3
Posts: 196
Joined: Thu Feb 16, 2017 10:40 pm

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

Post by ColdBoot » Sun Mar 05, 2017 12:11 pm

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.
Linux Mint Cinnamon 18.1
Intel G1820, DDR3 8GB, Nvidia GT720(2GB)

edcompsci
Level 1
Level 1
Posts: 34
Joined: Fri Dec 21, 2012 5:25 pm

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

Post by edcompsci » Wed Jul 11, 2018 11:23 pm

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.

Post Reply

Return to “Scripts & Bash”