Ubuntu Server 8.04自动关机

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
chenghs
帖子: 13
注册时间: 2009-02-14 4:57

Ubuntu Server 8.04自动关机

#1

帖子 chenghs » 2009-02-21 10:39

我的系统是 Ubuntu server 8.04,没装x. 主要功能是做一个家用的文件服务器,所以不需要24小时都开着. 我想让server自动检测自己的空闲状态, 如果发现自己空闲了超过5分钟, 就自动关机或者休眠. 关键的是我需要服务器自己检测, 整个过程不需要我的干预. 查了网上 n 多的贴子, 最后参考了网上一个贴子 (http://ubuntuforums.org/showthread.php?t=530973) 做的. 但是系统没有没隔15分钟自动运行我的脚本, 哪为给看看是啥问题. 能否也帮我看看脚本是否合理. 谢谢了.!!!!

1. 我用 sudo nano /etc/crontab 在系统的 cron 中加了一句:
*/5 * * * * root /etc/acpi/sleep.sh (每5分钟运行一次sleep.sh脚本)

( 我单独运行脚本是正常的. 但是系统并没有自动每隔5分钟运行我的脚本. 好象根本就没有运行.)

2. 在 sudo visudo 加了一句:
sean ALL=NOPASSWD: /etc/acpi/sleep.sh

3. 在 /etc/acpi 下建立 sleep.sh 脚本: (原贴见: http://ubuntuforums.org/showthread.php?t=530973)
然后 sudo chmod +x sleep.sh
脚本

#!/bin/bash
#
# This is scheduled in CRON. It will run every 20 minutes
# and check for inactivity. It compares the RX and TX packets
# from 20 minutes ago to detect if they significantly increased.
# If they haven't, it will force the system to sleep.
#

log=~/Scripts/idle/log

# Extract the RX/TX
rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`

#Write Date to log
date >> $log
echo "Current Values" >> $log
echo "rx: "$rx >> $log
echo "tx: "$tx >> $log

# Check if RX/TX Files Exist
if [ -f ~/Scripts/idle/rx ] || [ -f ~Scripts/idle/tx ]; then
p_rx=`cat ~/Scripts/idle/rx` ## store previous rx value in p_rx
p_tx=`cat ~/Scripts/idle/tx` ## store previous tx value in p_tx

echo "Previous Values" >> $log
echo "p_rx: "$p_rx >> $log
echo "t_rx: "$p_tx >> $log

echo $rx > ~/Scripts/idle/rx ## Write packets to RX file
echo $tx > ~/Scripts/idle/tx ## Write packets to TX file

# Calculate threshold limit
t_rx=`expr $p_rx + 1000`
t_tx=`expr $p_tx + 1000`

echo "Threshold Values" >> $log
echo "t_rx: "$t_rx >> $log
echo "t_tx: "$t_tx >> $log
echo " " >> $log

if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then ## If network packets have not changed that much
echo "Suspend to Ram ..." >> $log
echo " " >> $log
rm ~/Scripts/idle/rx
rm ~/Scripts/idle/tx
sudo /etc/acpi/sleep.sh force ## Force Sleep
fi

#Check if RX/TX Files Doesn't Exist
else
echo $rx > ~/Scripts/idle/rx ## Write packets to file
echo $tx > ~/Scripts/idle/tx
echo " " >> $log
fi
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

Re: Ubuntu Server 8.04自动关机

#2

帖子 bones7456 » 2009-02-21 10:43

1. 我用 sudo nano /etc/crontab 在系统的 cron 中加了一句:
你这个做法就是不对的,要用 crontab -e 编辑。试试吧
关注我的blog: ε==3
chenghs
帖子: 13
注册时间: 2009-02-14 4:57

Re: Ubuntu Server 8.04自动关机

#3

帖子 chenghs » 2009-02-21 11:25

我用了 sudo crontab -e , 还是没有运行我的脚本.会有其他原因吗? 麻烦帮看看了. 谢谢了啊...
头像
sevk
帖子: 2060
注册时间: 2007-05-08 16:26
系统: arch
来自: 火星内核某分子内某原子核内
联系:

Re: Ubuntu Server 8.04自动关机

#4

帖子 sevk » 2009-02-21 11:51

代码: 全选

ls -l /etc/acpi/sleep.sh
看看
笔记本 :
F208S : gentoo
A460P i3G D6 : UBUNTU + WIN7
UN43D1 : UBUNTU + WIN7
1000人超级QQ群 LINUX + WIN : 31465544 或 18210387
poet
帖子: 2841
注册时间: 2006-09-11 22:47

Re: Ubuntu Server 8.04自动关机

#5

帖子 poet » 2009-02-21 12:24

你这个脚本最好用 root 执行。

sudo 编辑时用的是本用户。建议你先 sudo -i 进入 root 用户,然后再 crontab -e 去编辑脚本。
chenghs
帖子: 13
注册时间: 2009-02-14 4:57

Re: Ubuntu Server 8.04自动关机

#6

帖子 chenghs » 2009-02-21 12:25

显示为 -rwxr-xr-x root root 1603 2009-02-20 21:08 /etc/acpi/sleep.sh
chenghs
帖子: 13
注册时间: 2009-02-14 4:57

Re: Ubuntu Server 8.04自动关机

#7

帖子 chenghs » 2009-02-21 12:30

按照您说的, 去了root, crontab里已经有 */2 * * * * root /etc/acpi/sleep.sh 这句话了. 不过还是存了一下, 仍然没有什么变化... 究竟是为什么不定时执行脚本呢?
poet 写了:你这个脚本最好用 root 执行。

sudo 编辑时用的是本用户。建议你先 sudo -i 进入 root 用户,然后再 crontab -e 去编辑脚本。
头像
JokerCai
帖子: 18
注册时间: 2008-12-18 16:46
来自: 广州大学华软软件学院

Re: Ubuntu Server 8.04自动关机

#8

帖子 JokerCai » 2009-02-24 15:18

编辑/etc/crontab 文件配置cron
  cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服务做一些事情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是:
  SHELL=/bin/bash
  PATH=/sbin:/bin:/usr/sbin:/usr/bin
  MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号
  HOME=/ //使用者运行的路径,这里是根目录
  # run-parts
  01 * * * * root run-parts /etc/cron.hourly //每小时执行/etc/cron.hourly内的脚本
  02 4 * * * root run-parts /etc/cron.daily //每天执行/etc/cron.daily内的脚本
  22 4 * * 0 root run-parts /etc/cron.weekly //每星期执行/etc/cron.weekly内的脚本
  42 4 1 * * root run-parts /etc/cron.monthly //每月去执行/etc/cron.monthly内的脚本
  大家注意"run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名了。
IT荒漠上的一迷途小小生,只想寻找到属于自己的一片绿洲
leisure
帖子: 141
注册时间: 2008-05-03 16:43
联系:

Re: Ubuntu Server 8.04自动关机

#9

帖子 leisure » 2009-08-18 16:50

脚本不是这样运行的,应该是
*/5 * * * * root bash /etc/acpi/sleep.sh
回复