如何关机?

sh/bash/dash/ksh/zsh等Shell脚本
长头发的和尚
帖子: 12134
注册时间: 2008-01-11 17:02

Re: 如何关机?

#16

帖子 长头发的和尚 » 2010-06-17 9:29

Z想干嘛
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 如何关机?

#17

帖子 aerofox » 2010-06-17 12:24

罗非鱼 写了:pt@pt-laptop:~$ sudo apt-get install gnome-power-manager
[sudo] password for pt:
Reading package lists... Done
Building dependency tree
Reading state information... Done
gnome-power-manager is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
pt@pt-laptop:~$ man gnome-power-cmd
No manual entry for gnome-power-cmd
有问题找 man 的想法不错,但是这个命令恰好是没有 manpage 的,不能因为没有 man 就断定不存在这个命令吧?为什么不直接试一下命令呢?
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 如何关机?

#18

帖子 aerofox » 2010-06-17 12:44

pocoyo 写了:没这个命令.
抱歉,刚才到 Ubuntu 10.04 中找一下,gnome-power-manager 软件包中确实不包含 gnome-power-cmd,Debian 5.0 中是包含的。
用 sudo 的方法吧,试过了吗?一定可以的。
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 如何关机?

#19

帖子 aerofox » 2010-06-17 12:46

Debian 中的 gnome-power-cmd 命令是个脚本文件,内容如下,估计放到 Ubuntu 中也可以用:

代码: 全选

#!/bin/sh
# Copyright (C) 2007 Richard Hughes <[email protected]>
#
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

#$1 = method name
execute_dbus_method ()
{
	dbus-send --session --dest=org.freedesktop.PowerManagement		\
		  --type=method_call --print-reply --reply-timeout=2000	\
		  /org/freedesktop/PowerManagement 			\
		  org.freedesktop.PowerManagement.$1
	if [ $? -eq 0 ]; then
		echo "Failed"
	fi 
}

if [ "$1" = "suspend" ]; then
	echo "Suspending"
	execute_dbus_method "Suspend"
elif [ "$1" = "hibernate" ]; then
	echo "Hibernating"
	execute_dbus_method "Hibernate"
elif [ "$1" = "reboot" ]; then
	echo "Rebooting"
	execute_dbus_method "Reboot"
elif [ "$1" = "shutdown" ]; then
	echo "Shutting down"
	execute_dbus_method "Shutdown"
elif [ "$1" = "" ]; then
	echo "command required: suspend, shutdown, hibernate or reboot"
else
	echo "command '$1' not recognised, only suspend, shutdown, hibernate or reboot are valid"
	exit 1
fi
Bill Lee
帖子: 598
注册时间: 2009-09-19 20:50

Re: 如何关机?

#20

帖子 Bill Lee » 2010-06-18 21:57

我说的很清楚了

代码: 全选

# !/bin/bash
do
        if [ -e "~/shutdownLock" ]; then
                sleep 10
        else
                shutdown -h now
        fi
done
保存为 TestShutDown.sh
然后

代码: 全选

chmod u+x TestShutDown.sh
sudo chown root\: TestShutDown.sh
sudo ./TestShutDown.sh
再执行 sudo -k 清除 sudo 密码缓存
只有 TestShutDown.sh 会以 root 执行。而你原来的脚本并没有 root 权限。另外修改 TestShutDown.sh 也需要 root 权限,这样就可以防止 root 权限被盗用。
aMule 2.3.1 is coming...
真正动态的 amule-dlp is coming...
本人帐号在2011年1月被盗,在2011年1月3日17:19到2010年1月6日13:34之间本帐号发的一切帖子、短信等与本人无关!
amule-dlp 开发动态
amule-dlp on Google Code
WebArchiver - 基于 wget 的网页归档工具(PyQt4 GUI 前端)
Linux 2.6.34 编译笔记
头像
erol
帖子: 61
注册时间: 2010-05-16 23:17

Re: 如何关机?

#21

帖子 erol » 2010-06-19 1:36

本来想说给shell脚本用suid的,做了一下实验发现我那ubuntu10.04上的bash 4.1.5不吃这一套...残念
用别方式的加上suid就应该行了吧
我很白,错了不要笑我...
伊吹风子的邻居常说:那孩子是个普通的地球人
mgqw
帖子: 59
注册时间: 2006-11-09 21:06
来自: 深圳

Re: 如何关机?

#22

帖子 mgqw » 2010-06-23 15:31

用超级用户创建shell脚本,然后修改此脚本的SUID,应该就可以了吧
only_one
帖子: 186
注册时间: 2010-05-25 16:49

Re: 如何关机?

#23

帖子 only_one » 2010-06-23 18:07

sudo chown root filename
sudo chmod u+s filename

刚刚学习shell 不知行不行 :em06
onelynx
帖子: 817
注册时间: 2008-11-13 16:03

Re: 如何关机?

#24

帖子 onelynx » 2010-06-24 21:11

直接拔电源就是了
罗非鱼
帖子: 980
注册时间: 2008-04-12 12:44

Re: 如何关机?

#25

帖子 罗非鱼 » 2010-06-27 7:58

各位谈呢这么多都没有到点子上,请问,linux(或者说ubuntu10.04)中,普通用户(没有root密码),可否用命令行关闭计算机??
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 如何关机?

#26

帖子 aerofox » 2010-06-27 8:55

上面给出了几种方法,你偏不用,还想要什么办法呢?
hongszh
帖子: 25
注册时间: 2006-11-08 11:12

Re: 如何关机?

#27

帖子 hongszh » 2010-06-28 8:53

aerofox 写了:知道 sudo su,为什么不直接 sudo shutdown 呢?

代码: 全选

visudo
然后加入一行:

代码: 全选

%sudo ALL=NOPASSWD: /sbin/shutdown
然后脚本中就可以用

代码: 全选

sudo /sbin/shutdown -h now
如果用 gnome 环境,也可以使用命令:

代码: 全选

gnome-power-cmd shutdown
我是这么操作的,类似上面,先加上NOPASSWD,这样sudo就不需要输入密码了:

代码: 全选

%admin ALL=(ALL)NOPASSWD:NOPASSWD: ALL
然后就可以使用init 0了:

代码: 全选

sudo init 0
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 如何关机?

#28

帖子 lilydjwg » 2010-06-28 21:03

罗非鱼 写了:各位谈呢这么多都没有到点子上,请问,linux(或者说ubuntu10.04)中,普通用户(没有root密码),可否用命令行关闭计算机??
aerofox 的就是了,使用 dbus 发个关机信号,没其它用户登录的话就立即给你关机了。
罗非鱼
帖子: 980
注册时间: 2008-04-12 12:44

Re: 如何关机?

#29

帖子 罗非鱼 » 2010-06-29 19:04

这帮回帖的,一个比一个不务实,我的前提条件是,没有sudo 的密码,没有root密码,普通用户根本不能使用那些命令!
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 如何关机?

#30

帖子 lilydjwg » 2010-06-29 22:06

罗非鱼 写了:这帮回帖的,一个比一个不务实,我的前提条件是,没有sudo 的密码,没有root密码,普通用户根本不能使用那些命令!
你才不务实呢!普通用户可以发dbus消息!以下是我的关机函数:

代码: 全选

shutdown () { #{{{2
  echo -n 你确定要关机吗?
  read i
  if [ $i = "y" -o $i = "是" ]
  then
    dbus-send --system --print-reply --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown
  fi
}
回复