LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-11-2005, 07:30 AM   #1
cscott
Member
 
Registered: Apr 2004
Location: Nashville, TN
Distribution: Gentoo, Mandrake 10.1 Official
Posts: 50

Rep: Reputation: 15
How do you automatically power off after a program finishes?


I'm using acpi, and would like to know how to automatically power off my desktop computer after a program finishes. For example, most of the time I am in the console or shell compiling programs that take an unknown amount of time. I would like for the system to do an "/sbin/halt" whenever the program that's running in the console finishes. Does anyone know how to do this?
 
Old 03-11-2005, 09:17 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
I've written a small script to do just that. It is available on my web site:
http://yves.gablin.club.fr/pc/linux....ticle=download

Yves.
 
Old 03-11-2005, 09:55 AM   #3
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
in the simpliest case if the user who is running the program has permission to execute halt, and the program is not a daemon(it is attached to the console)
Something like:

<execute program>; /sbin/halt
will halt the pc after the program has finished.
e.x.
Code:
cdrecord -v -eject speed=2 dev=0,1,0 -dao file.iso ; /sbin/halt
this should turn off the pc after the cd is burned, since the commands are executed serial and /sbin/halt
won't be executed until the console is unblocked by cdrecord. I've never tried it but I cannot think of a reason why this should not work.

Last edited by perfect_circle; 03-11-2005 at 09:59 AM.
 
Old 03-13-2005, 07:50 AM   #4
cscott
Member
 
Registered: Apr 2004
Location: Nashville, TN
Distribution: Gentoo, Mandrake 10.1 Official
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks, I've been using something like this say in compiling a program... make && make install && /sbin/init 0... which works, but I wanted something that would do it automatically and not require me to entire a shutdown or halt command such as init 0. For example, I'm trying to figure out how to monitor the console for silence or monitor all running programs, make sure everything is finished, and then issue a shutdown.

That cdrecord example was a good one. I wish all programs had an option that allowed you to check whether you wanted to shutdown your machine after the program finished.

Also... I couldn't figure out how to get that suspend script on YinYeti's site to work. I did get an idea from your script about using ps -aux to monitor for activity, though I'm not quite sure how to implement it..

Last edited by cscott; 03-13-2005 at 07:58 AM.
 
Old 03-13-2005, 08:18 AM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Quote:
Originally posted by cscott

That cdrecord example was a good one. I wish all programs had an option that allowed you to check whether you wanted to shutdown your machine after the program finished.

Also... I couldn't figure out how to get that suspend script on YinYeti's site to work. I did get an idea from your script about using ps -aux to monitor for activity, though I'm not quite sure how to implement it..
This has nothing to do with cdrecord command but with bash.
if you do
Code:
cdrecord -v -eject speed=2 dev=0,1,0 -dao file.iso & /sbin/halt
to tell the cdrecord to run in the background, affter cdrecord starts the console will be freed and the /sbin/halt will start right after cdrecord has started. So this will try to halt the pc right away and the cd you started burning will be destroyed.

Last edited by perfect_circle; 03-14-2005 at 03:36 AM.
 
Old 03-14-2005, 02:51 AM   #6
cscott
Member
 
Registered: Apr 2004
Location: Nashville, TN
Distribution: Gentoo, Mandrake 10.1 Official
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by perfect_circle
This has nothing to do with cdrecord command but with bash.
if you do
Code:
cdrecord -v -eject speed=2 dev=0,1,0 -dao file.iso &; /sbin/halt
to tell the cdrecord to run in the background, affter cdrecord starts the console will be freed and the /sbin/halt will start right after cdrecord has started. So this will try to halt the pc right away and the cd you started burning will be destroyed.
Huh? I don't understand. In your first post you suggested this code, now you are saying that it doesn't work and will destroy the CD.

Anyhow, I'm still looking for a way to automatically turn off my pc after all unattended programs are finished. Maybe I need to look at bash's job control for a solution.
 
Old 03-14-2005, 03:13 AM   #7
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Hello,

I'm sorry, I was not precise enough. I was not refering to the EPIA suspend script, which is directed at the EPIA mainboard, and if for a manual suspend anyway.
I was rather refering to the very little "watch.sh" script.

Now I see from your later posts, that you don't really know what should be watched for. It's more a matter of knowing when the PC can "safely" be shutted down.
That's something I've been thinking about, but I've done nothing in this area yet. In my case (living-room PC), I think I'll watch for a limited list of programs (namely: Xine, Xawtv, streamer, and some scripts of mine), and suspend or halt the machine if none of those are running, and if 'at' and 'cron' report nothing to do in the following hour or so.

Yves.
 
Old 03-14-2005, 03:36 AM   #8
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Quote:
Originally posted by cscott
Huh? I don't understand. In your first post you suggested this code, now you are saying that it doesn't work and will destroy the CD.

Anyhow, I'm still looking for a way to automatically turn off my pc after all unattended programs are finished. Maybe I need to look at bash's job control for a solution.
No!!! In my post i suggested this : cdrecord -v -eject speed=2 dev=0,1,0 -dao file.iso ; /sbin/halt

Without & in the end of cdrecord command. When you put & in bash this means run the command in the background.
When put & the process becomes a daemon. It is not attached to the console any more (well, not exactly) and the prompt is returned to the console and you may execute another command in the console, without having terminated the previous command. So /sbin/halt, will be executed write after cdrecord is executed, without leting cdrecord finish.

Actually when you put & you shouldn't put ; because it's wrong, so the second command I posted wouldn't work anyway. I'll correct it in the post.
To see the difference try this:
Code:
xmms; echo "echo has just been executed"
And
Code:
xmms & echo "echo has just been executed"
In the first command the echo part will be executed after you close xmms.
In the second command the echo part will be executed right after xmms starts.

The problem is that some programs are daemons be default and in those programs the
<program>; <program> scheme won't work.

Also check theYinYeti's watch.sh script. It may do the job for you.
Example:
Build a simple script to test it like my test_watch script.
Code:
skalkoto@darkstar:~$ cat test_watch
#!/bin/bash
while [ 1 ]; do
   sleep 1
done
And execute it. This contain a dummy loop that will never stop until u kill it(CTRL +C). Open a condole and execute the script.
Then open another console and execute theYinYeti's watch.sh after you make sure you replace bell.sh with echo "$1 program has finished" like this:
Code:
skalkoto@darkstar:~$ cat  watch.sh
#!/bin/bash
# This script scans the output of "ps -e" every $1 seconds, until grep
# does not find $2 in this output anymore, then bell.sh is called.
#
# Copyright (C) 2004,2005 Yves Gablin (gablin@fr.fm)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation: http://www.gnu.org/licenses/gpl.html
#
# Parameters:
# $1 : "sleep" parameter (see "sleep" man page)
# $2 : "grep" parameters (this is a regular expression)
#
# Usage examples:
# watch.sh 1m make  # scan every minute
# watch.sh 15 make  # scan every 15 seconds

this="$(basename "$0")"
sleeptime=$1
shift

while [ -n "$(ps -e -o cmd | grep "$@" | grep -vF "$this" | grep -vF grep)" ]; do
        sleep $sleeptime
done

#bell.sh
echo "$1 program has finished"
skalkoto@darkstar:~$
and
Code:
skalkoto@darkstar:~$ ./watch.sh 1 test_watch
The echo will be executed after test_watch has finished/killed
If you replace the echo with /sbin/halt this will be executed after the program you are watching has finished

Last edited by perfect_circle; 03-14-2005 at 03:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Power Wont turn off automatically marigb Linux - Hardware 5 11-16-2005 08:08 PM
The computer does not power down automatically xcalibra Red Hat 2 10-04-2004 11:07 PM
Kdevelop splash screen finishes loading then program crashes BluePyre Linux - Software 1 08-25-2004 01:51 PM
Controlling desktop power, so that it doesn't go into standby automatically. dmccol Mandriva 6 05-03-2004 07:05 PM
how to automatically power off after shutdown in redhat7.3(kernel 2.4.20) prad Linux - General 8 12-18-2002 02:37 PM


All times are GMT -5. The time now is 12:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration