当前位置:首页 > 未命名 > 正文

编写脚本让树莓派每天0点重启桌面和lnmp来释放内存

树莓派内存老是满,元凶竟是桌面服务!

让我们来整一波骚操作:

脚本如下:

root@raspberrypi:/usr/local/shell# cat clearMemCache.sh 
#!/bin/bash
cd
sudo lxpanelctl restart
sleep 1
sudo service lightdm restart
sleep 1
sudo lnmp restart
print "释放内存完成"

输入: crontab -l来查看当前有的计划任务

root@raspberrypi:/usr/local/shell# crontab -l
#Edit this file to introduce tasks to be run by
30 * * * * echo 1 > /proc/sys/vm/drop_caches

# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

然后我们查看shell脚本的路径 输入pwd

root@raspberrypi:/usr/local/shell# pwd
/usr/local/shell
root@raspberrypi:/usr/local/shell#

接着输入ls

root@raspberrypi:/usr/local/shell# ls
clearMemCache.sh
root@raspberrypi:/usr/local/shell#

然后我们再计划任务中写入任务

root@raspberrypi:/usr/local/shell# crontab -e
crontab: installing new crontab
root@raspberrypi:/usr/local/shell# crontab -l
#Edit this file to introduce tasks to be run by
30 * * * * echo 1 > /proc/sys/vm/drop_caches
0 0 * * *  /usr/local/shell/clearMemCache.sh 
#上面这一行就是每天0点执行的脚本地址
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

我们可以手动执行这个脚本看看效果

root@raspberrypi:/usr/local/shell# ./clearMemCache.sh 
Cant connect to display: (null)
+-------------------------------------------+
|    Manager for LNMP, Written by Licess    |
+-------------------------------------------+
|              https://lnmp.org             |
+-------------------------------------------+
Stoping LNMP...
Stoping nginx...  done
[ ok ] Stopping mysql (via systemctl): mysql.service.
Gracefully shutting down php-fpm . done
Starting LNMP...
Starting nginx...  done
[ ok ] Starting mysql (via systemctl): mysql.service.
Starting php-fpm  done
Error: no such file "释放内存完成"
root@raspberrypi:/usr/local/shell#

然后就OJBK,明天看看效果

更新时间 2021-05-20