LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-09-2003, 07:45 AM   #1
Alfastation
LQ Newbie
 
Registered: Oct 2003
Location: Belgium
Posts: 7

Rep: Reputation: 0
Pls help me write script to shutdown pc


Pls help me write a short script which shuts down the computer.

The idea is to have a shortcut on my desktop which, when i double-click on it as a normal user, launches a small executable that automaticly shuts down the computer.
So I guess the script should read something like this:

su -
type password "xxxx"
shutdown -h now

Since i'm absolutely new to Linux and have no xp with programming pls be gentle with me!

(Oh, and im' running Xebian 5.1 on an Xbox in a XFCE4 graphical desktop environment).

Thanks,
-Alfa-
 
Old 11-09-2003, 08:32 AM   #2
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
You should consider reading up a bit on shell programming. A shell script can be just a simple collection of commands, or an advanced program. Anyhow, it's great for getting things done. Mendel Cooper's Advanced Linux Bash-scripting Guide is an excellent resource and I always keep a copy on my laptop in case I need it.

Hint: What you want is probably to use Xdialog (to display input and output windows) and expect (to pass the password to su).

Håkan
 
Old 11-09-2003, 08:46 AM   #3
DirtDart
Member
 
Registered: Nov 2003
Distribution: Mandrake 10.1/Solaris 10 (sparc)
Posts: 96

Rep: Reputation: 16
Assuming you have root access, and another account, you can simply give your non-root account access to shutdown/reboot the machine.

Pretty easy to do in Mandrake, but not really sure about your distro. I do believe that there is this option in good old reliable linuxconf
 
Old 11-09-2003, 10:04 AM   #4
Alfastation
LQ Newbie
 
Registered: Oct 2003
Location: Belgium
Posts: 7

Original Poster
Rep: Reputation: 0
Let me repeat my Q:

The idea is to have a shortcut on my desktop which, when i double-click on it as a normal user, launches a small executable that automaticly shuts down the computer.

('cause i'm lazy and i don't want to use a keyboard on the Xbox !)

So,
Thanks for the tips, but i was really hoping for an answer along the line of: ...write this..., do that... , and it should work...
No offence but right now i dont have the time to read a 50 page manual (even tho i sure will read it soon...looks good at first sight).

-Alfa-
 
Old 11-10-2003, 08:21 AM   #5
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
OK then, here's one I whipped up. My scripting sucks, but it works on my box. Requires expect and Xdialog.
Code:
#!/bin/bash
# Author: Håkan Wikström <hn_wikstrom@hotmail.com>
# Purpose: Display a dialog box asking for root password and then shut down the computer
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
Xdialog --title "Shutting down" --screen-center --passwordbox "Enter root password" 10 40 2> $tempfile
retval=$?
case $retval in
  0)
    pw=`cat $tempfile`
    Xdialog --title "Output" --screen-center --msgbox "OK, shutting down..." 10 40
    expect -d - << EndMark
        spawn "/bin/bash"
	send "su\r"
        expect -re "Password: "
	sleep 1
        send "$pw\r"
	expect -re "#"
        send "/sbin/halt\r"
	expect -re "#"
EndMark
    ;;
  1)
    Xdialog --title "Cancelled" --screen-center -msgbox "Cancel pressed, aborting." 10 40
    ;;
esac
Håkan

Last edited by hw-tph; 11-10-2003 at 08:25 AM.
 
Old 11-10-2003, 11:04 AM   #6
qwijibow
LQ Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
Let me repeat DirtDart's answer in different words...
(lol)

open console.

su
[root_passwd]
cd /bin
ln -s /sbin/shutdown
chmod 555 /sbin/shutdown
chmod 555 /bin/shutdown
exit

what we have just done, is made a symbolic link to the shutdown command in
the standard bin path, (in all users $PATH variable)
and given all users read and execute access to both the shutdown binary, and the symbolic link (not sure if u need to do it for the link, but it cant hurt)

now... slap an icon on your desktop, and make the icon execute the command
`shutdown -h now`

boom, you have an icon what will shut down up PC.

and yeah okay, its not as good as hw-tph's solution, but gets the job done.
(plus, you dont need to give all users the root password just to let em shutdown )
 
Old 11-10-2003, 05:26 PM   #7
Alfastation
LQ Newbie
 
Registered: Oct 2003
Location: Belgium
Posts: 7

Original Poster
Rep: Reputation: 0
Ok, I think i'm starting to dislike Linux allready.
I do however greatly appreciate your answers, pls don't get me wrong.

@qwijibow:

Sorry but your method doesn't seem work; I keep getting the message "must be root to use shutdown", even tho' i gave r/w/x permission to all users (used chmod 555, 757, a+rwx....nothing works)
Next i tried the same method on the "halt" command. Here i'm getting: "must be superuser to use halt".
So a friend suggested to add my username to "shutdown.allow"; need i say that didn't resolve it either?!
Oh, and did i mention i'm unable to create an icon/shortcut on the desktop? Seems i can only create them in the "taskbar" on the bottom.

@hw-tph:

Thanks for your script, but the whole point is NOT to need a keyboard to shut down the system, where-as typing a password still requires a keyboard... correct me if i'm wrong But thanks anyway for the script. I'll try it out later, if & when i find the time to figure out the basics ...


On a side note, I tried shutting down the system by simply pressing the power-off button (it's become a habit, used to win98 freezing up completely at least once a week for the past 5 years ), and i ended up re-installing Linux from scratch 'cause at the next power-up my desktop was all screwed-up (the icons, shortcuts, etc... on the bottom of the screen were all gone! )

Maybe I should get that mod-chip after all...

-Alfa-
 
Old 11-10-2003, 09:12 PM   #8
qwijibow
LQ Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
mod chip ?

well, i suppose there are good reasons to prevent standard users shutting down.

what wm are you using ?
usually the wm provides a shutdown method...

in icewm its ctrl-alt-del then shutdown.

in kde you can click log-out, and then shutdown.

you cannot creat icons... why ???

try chown -R [username] /home/[username]
to make sure you are the owner of ur home directory.

anyway......
hey, as far as ive seen hw-tph's solution dont require you to use the keyboard.
just put his code into a file, chmod it to be executable.
then put a icon shortcut to it.
click the icon, it will prompt for a password, then down it goes !

just make sure you have the dependency's he notes at the top of his post.
 
Old 11-10-2003, 10:39 PM   #9
Demonbane
LQ Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
try as root
chmod +s /sbin/halt

then just put a very simple script on the desktop with the following content:

sh /sbin/poweroff
 
  


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
Trouble running firewall script - pls help Wags Linux - Security 1 07-29-2004 09:19 AM
Netconfig & floppy write probs-pls help! jhecht Linux - Software 0 06-27-2004 03:23 PM
cfdisk'in using slackware.. but no permission to write.. PLS HELP! r3dxP Slackware - Installation 1 05-27-2004 02:01 PM
Simple firewall script. pls help Wags Slackware 5 02-18-2004 05:13 PM
pls pls pls help me ! i'm tired with httpd config on fedora apache 2.0.48 AngelOfTheDamn Fedora 0 01-24-2004 06:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration