• 利用mac的launchd开机后定时启动shell脚本

    • shell脚本
    • mac开机启动
      • 概念
      • 配置Mac开机后定时启动
    • 参考文档

利用mac的launchd,开机后定时启动shell脚本

利用mac os x的launchd,开机后定时启动shell脚本,并且周期执行shell命令
shell脚本内容:先检测ssh代理是否被使用,如没有使用,则重启本地ssh代理

shell脚本

touch $HOME/.ssh.agent.sh
chmod 755 $HOME/.ssh.agent.sh
vi $HOME/.ssh.agent.sh
#!/bin/bash
port=2022;
remote="-i $HOME/remote_login/pem/amazon_free_li.pem root@52.68.78.183";
function restart(){acitve=`netstat -an |grep $port|grep ESTABLISHED |wc -l`;if [ $acitve -gt 0 ]thenecho "`date` :already ESTABLISHED $acitve ,no need to restart";elseecho "`date` :restart ssh agent";ps -ef | grep $port|grep qTfnN | grep -v grep | awk '{print $2}' | xargs kill -9ssh -qTfnN -D $port $remotefi
}function dowhile(){while truedo restart ;    sleep 600;done
}restart ;

mac开机启动

概念

background program is a program that runs in the background, without presenting any significant GUI. This category is subdivided into daemons (system wide background programs) and agents (which work on behalf of a specific user). The next two sections describe these subdivisions in detail.

A daemon is a program that runs in the background as part of the overall system (that is, it is not tied to a particular user). A daemon cannot display any GUI; more specifically, it is not allowed to connect to the window server. A web server is the perfect example of a daemon.

Note: If you’re coming from a traditional UNIX background, be aware that modifying /etc/rc* is not a supported way of launching a daemon on Mac OS X. Rather, you should launch your daemon via launchd.

An agent is a process that runs in the background on behalf of a particular user. Agents are useful because they can do things that daemons can’t, like reliably access the user’s home directory or connect to the window server.
The difference between an agent and a daemon is that an agent can display GUI if it wants to, while a daemon can’t. The difference between an agent and a regular application is that an agent typically displays no GUI (or a very limited GUI).

A launchd agent is like a launchd daemon, except that it runs on behalf of a particular user. It is launched by launchd, typically as part of the process of logging in the user.

A third party launchd agent should be installed by adding a property list file to the ~/Library/LaunchAgents directory (to be invoked just for this user) or /Library/LaunchAgents directory (to be invoked for all users).

A login item is launched when the user logs in using the GUI. A login item can be any openable item, but it is typically an application or an agent

配置Mac开机后定时启动

根据上述概念,需要用户登录后,定时启动,所以对应/Library/LaunchAgents(所有用户)或~/Library/LaunchAgents(单独用户)

cd ~/Library/LaunchAgents # Per-user agents provided by the administrator.
vi com.lcz.ssh-agent.plist

com.lcz.ssh-agent.plist配置内容如下
说明:在plist中要写绝对路径,~或者$HOME验证不过

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>Label</key>    <string>com.lcz.ssh-agent</string><key>ProgramArguments</key><array><string>$HOME/.ssh.agent.sh</string></array><key>StartInterval</key>  <integer>600</integer><key>RunAtLoad</key> <true/><key>KeepAlive</key> <false/><key>StandardErrorPath</key> <string>/tmp/AlTest1.err</string><key>StandardOutPath</key> <string>/tmp/AlTest1.out</string></dict>
</plist>
launchctl load com.lcz.ssh-agent.plist #加载一个配置文件,Jobs立即启动
launchctl list |grep ssh #list all of the jobs loaded into launchd i
launchctl unload com.lcz.ssh-agent.plist

参考文档

Daemons and Agents :概念科普类文档,仔细读一篇,理论会有很大提升.
Mac crontab - Mac OS X startup jobs with crontab, er, launchd:使用经验,如Label 和文件名最好要符合命名规范,以免冲突
Creating Launch Daemons and Agents:官方泛泛文档,质量一般。不如前面那篇概念科普类文档
launchctl使用手册
launchd.plist使用手册

利用mac的launchd,开机后定时启动shell脚本相关推荐

  1. 【windows脚本】开机后定时重启

    环境 系统:win10 x64(1809) 概述 使用windows脚本实现定时重启windows系统,重启系统后运行一段时间再重启,循环罔替. 1.跳过windows登录界面 跳过windows登录 ...

  2. 电脑开机后无法启动出现的0xc0000428错误

    电脑开机后无法启动出现的0xc0000428错误 这种情况遇到过很多次,一般都是卸载或者是更改驱动之后的出现的无法启动的错误. 直接上图: 当我们遇到这个问题的时候我们怎么办呢? 问题的解决(进入系统 ...

  3. linux下开机自动启动,定时运行shell脚本

    Shell 脚本与window/dos 下的批处理相似,也就是用各类命令预先放入一个文件中,方便一次性执行的的一个程序文件,主要是方便管理员进行设置或者管理用. 一.shell脚本 在Linux下,我 ...

  4. Android8.0 开机启动脚本,Android开机启动shell脚本(Android 8.0测试OK)

    Android 下做开机启动shell脚本的大致流程如下: 目录 写shell脚本 为脚本写te文件 在init.rc中启动脚本 添加Selinux权限 写shell脚本 比如新建一个init.tes ...

  5. Android 开机启动shell脚本

    接到一个集成功能的需求,然后看了一下是由上层应用 + linux进程实现的功能,需要增加开机自动启动linux进程,没弄过有点懵. 这个不怎么正确,仅供参考,在权限那块需要更改,放到system下 环 ...

  6. Linux 定时执行shell 脚本

    2019年第 85 篇文章,总第 109 篇文章 本文大约2000字,阅读大约需要6分钟 crontab 可以在指定的时间执行一个shell脚本以及执行一系列 Linux 命令. 定时执行shell ...

  7. Android init.rc启动shell脚本

    init.rc启动shell脚本 0. 前言 1. 编写脚本 test.sh 2. 修改 .mk 配置文件,将创建的 test.sh 编译到系统分区 3. 配置 SELinux 权限 3.1 创建 t ...

  8. Java程序定时执行shell脚本

    第一次写博客,写的不好还请见谅. 之前在Linux环境中想定期执行某个脚本,第一反应就是将这个task加入到crontab里(crontab的知识点这里就不具体介绍了),当然,这种做法一般情况下是可行 ...

  9. linux定时执行shell脚本

    linux定时执行shell脚本 需求:每分钟检查下文件是否存在 解决思路: 1.编写shell脚本,检查文件是否存在,存在在文件中记录yes,不存在记录no 2.将脚本加入linux定时任务cron ...

最新文章

  1. java 中文分词 比较_中文分词工具评估:chinese-segmentation-evaluation
  2. 《研磨设计模式》chap24 桥接模式bridge(1)基本概念
  3. 「Jupyter」ubuntu下安装jupyterlab后jupyterlab:未找到命令
  4. Python3.7版本unittest框架添加用例的方法
  5. TagHelper是怎么实现的
  6. 递归Java_递归的Java实现
  7. java 内存 四_java最终化的内存保留问题(4)
  8. github api常用操作
  9. python 无法读取文件 找不到文件
  10. 手机怎么解决同ip多账号_中海达RTK连接CORS账号后一直显示单点怎么解决?
  11. C# where()筛选方法
  12. 用C语言写Badapple
  13. 企鹅日记(十一):账号管理与ACL权限设置
  14. 不规则物体抓取机械手机械臂
  15. 简单了解ACL与NAT!
  16. java 参数中含有… 是什么意思
  17. 使用node+vue.js实现SPA应用,解决了SPA应用的最大缺点SEO
  18. Moonbeam生态说|Hello! Wormhole
  19. 【Css】使用float:left浮动后,导致后面div高度“塌陷”的解决办法(示例和图示)
  20. 文章管理平台PC端(文章分类)

热门文章

  1. glew java_使用GLEW在Windows下使用OpenGL扩展
  2. oracle中的时间比较大小,Oracle 时间比较
  3. 命令查询hdfs目录下文件总和
  4. RegexBuddy设置字体,大小
  5. 南阳88--汉诺塔(一)
  6. luogu插件:鼠标点击特效
  7. vue2+vant 开发公众号
  8. 京东搜索权重新规 京东搜索权重衰退模型解读
  9. 全双工、半双工、单工有什么区别!
  10. 查看inux操作系统环境