bash+expect来处理下载完关机

逍遥云 posted @ 2011年3月23日 08:26 in Linux世界 with tags bash shutdown 下载完自动关机 expect , 4857 阅读

昨天在用ff从tgbus上下载几个psp游戏,然后在想定个时在什么时候关机

sudo shutdown -h +320

但是一想,如果这破电信的网速不好,那岂不是白搞了(不知115是否能断点续传),然后就想到搞个下载完关机的sh脚本。

这里sh脚本我就不说了,思路是:

while循环

    sleep两秒

    如果测试没有以part结尾的文件名的话

        关机

想到这些我就写下了这样的东东

#!/bin/bash
while $(sleep 2);
do
    if [ ! -f *.part ]; then # 测试当前目录下有无firefox未下载完的文件
        sudo shutdown -h now
    fi
done

后来觉得有一问题,就是要管理员的密码。进而就想到了使用expect, 然后就把shutdown那句放入了命名为power-off.expect文件中。

#!/usr/bin/expect
set timeout 60
spawn sudo shutdown -h +1 # 推迟1分钟,以防文件还未写完。
expect "leon:" # 这句是猜测执行上段命令回显的最后几个字符,大概就是"password for user:"或 "Password:"或"密码:"
send "yourpassword\r"
interact

注意:这里的power-off.expect里面你的密码就是明文了,一定要注意权限,我是直接700了(即同组与其它组无读,无写,无执行权限)。

然后,将power-off.expect放入要调用的执行脚本,即

#!/bin/bash
while $(sleep 2);
do
    if [ ! -f *.part ]; then # 测试当前目录下有无firefox未下载完的文件
        ./power-off.expect
    fi
done

 

Avatar_small
依云 说:
2011年3月23日 16:07

为什么不用 dbus 消息来关机呢?

Avatar_small
逍遥云 说:
2011年3月24日 02:47

还没玩过dbus, 呵呵。

Avatar_small
gfreezy 说:
2011年3月30日 15:19

一直是直接
wget -c url ;shutdown -h now

bash不熟啊

Avatar_small
NCERT GK Sample Pape 说:
2022年9月17日 00:31

GK stands for General knowledge which was introduced as one of the subjects. It is beginning from the foundation education to help the students by getting minimum knowledge. GK also play a key role in competitive examinations, NCERT GK Sample Paper Class 2 that’s the way NCERT included General knowledge is a subject that everyone should know and learn from Primary Education.Downloading NCERT GK Sample Paper 2023 Class 2 utilizes candidates who have to know the new examination pattern and want to get high scores smoothly for all formats of exams conducted in Term-1, Term-2 such as SA-1, SA-2, FA-1, FA-2, FA-3, FA-4, Assignments and other types of exams to Hindi Medium, English medium and Urdu Medium students.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter