树莓派开机自动运行shell脚本——记录开机时间

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_25908839/article/details/87894445

目录

1.目的

2.思路

3.实现步骤

1.目的:树莓派开机时自动执行某些脚本,不用手动设置,减少麻烦。

2.思路:新建一个记录时间的脚本 record_time.sh,然后添加执行命令到 /etc/rc.local文件中

3.实现步骤

(1) 新建一个 bash shell 脚本

$ touch /home/pi/autoExec/record_time.sh 

(2) 往 record_time.sh 脚本添加如下内容

#!/bin/bash

path="/home/pi/autoExec"

time=$date

touch $path/time.log  #新建记录时间的日志 time.log

chmod 777 $path/time.log  #给 time.log 添加权限

echo $time >> $pwd/time.log  #往 time.log 输入时间

(3) 给予脚本 record_time.sh 权限并执行,得到 time.log,查看time.log

$ chmod 777 record_time.sh
$ bash ./record_time.sh
$ cat time.log
2019年 02月 23日 星期六 20:00:05 CST

(4) 在 /etc/rc.local 文件中添加执行脚本命令

# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

bash ./home/pi/autoExec/record_time.sh  #添加这条命令,路径是绝对路径

exit 0

(5) 查看time.log得

2019年 02月 23日 星期六 20:00:05 CST

2019年 02月 23日 星期六 20:06:17 CST

结论:结果表明,记录重启时间成功

猜你喜欢

转载自blog.csdn.net/qq_25908839/article/details/87894445