做一个很简单的开机脚本,用途:

1、得到开机引导时间

2、弹出窗口显示出来,类似windows上各种管家程序的提示一样

开机时间

这里直接查看/proc/uptime里记录的开机运行时间

# cat /proc/uptime 
47414.13 180787.43

第一个是我们要的(单位是秒):

# awk '{print $1}' /proc/uptime 
47468.06

打开窗口

# xterm -hold -e 'echo hello world' &

加&是让它在后台运行

再添加些颜色:

xterm -fa Monspace -fs 50 -hold -bg red -e 'echo 本次开机时间:30s;echo hell'

最后就可以写入开机脚本里了

然而上述过程是不行的,原因很简单,图形界面里用户是需要登录的,在登录之前什么都干不了

找到登录后执行的脚本

所以我们需要登录后执行的脚本,参考:http://www.jianshu.com/p/041564d86057

在下列三个里任意一个

/.bash_profile、

/.bash_login、

/.profile

经过验证,我的电脑是执行.profile

采用通知形式

上面采用xterm其实完全没必要,我们完全可以用系用通知:

notify-send "标题" "内容"

于是修改后的脚本 boot_time.sh:

#!/bin/bash

time=$(awk '{print $1}' /proc/uptime)

#xterm -fa Monspace -fs 40 -hold -bg red -e "echo 本次开机时间:$time s;echo 小J觉得很不错"

sleep 10s #这个延时是等待图形界面加载,我的大约是10s

notify-send "哈哈,$USER" "本次开机时间:$time s,我觉得棒棒哒"

exit 0

修改~/.profile,追加:

#boot time
bash ~/myshell/boot_time.sh &

这样在登录后就可以看到通知了

问题修复

我们要的是开机时间,然而/proc/uptime里存的是运行时间,假如我们很久才登录,这时候读取的时间就不是开机时间了,怎么办呢?

我采用文件存储的形式,在开机完成后将时间写入一个文件,登录后再读出来,所以程序如下:

get_boot_time.sh
------------------------
#!/bin/bash
###
#将开机时间写入文件,需要在开机完成的脚本/etc/rc.local里执行
###

time=$(awk '{print $1}' /proc/uptime)

#xterm -fa Monspace -fs 40 -hold -bg red -e "echo 本次开机时间:$time s;echo 小J觉得很不错"

#将时间写入文件
echo "$time" > ~/myshell/boot_time
show_boot_time.sh
-----------------
#!/bin/bash
###
#将开机时间读取出来,通过通知显示,在~/.profile里执行
###

time=$(cat ~/myshell/boot_time)

#xterm -fa Monspace -fs 40 -hold -bg red -e "echo 本次开机时间:$time s;echo 小J觉得很不错"

sleep 10s

notify-send "哈哈,$USER" "本次开机时间:$time s,我觉得棒棒哒"

exit 0

警告

什么?你觉得这样就完了?

在登录之前,~目录是不存在的,所以如果将脚本写在那里是根本不能被打开的,所以会得到如下错误(/var/log/system.log)

rc.local[861]: /var/get_boot_time.sh: 10: /var/get_boot_time.sh: 
cannot create ~/myshell/boot_time: Directory nonexistent

解决办法:改到var目录或者其他系统目录下

results matching ""

    No results matching ""