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

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

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

shell脚本touch $HOME/.ssh.agent.shchmod 755 $HOME/.ssh.agent.shvi $HOME/.ssh.agent.sh#!/bin/bashport=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`; ] ,no need to restart”; ;ps -ef | grep $port|grep qTfnN | grep -v grep | awk ‘{print $2}’ | xargs kill -9ssh -qTfnN -D }function dowhile(){ 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验证不过

=>Label>ProgramArguments>$HOME/.ssh.agent.sh>StartInterval>RunAtLoad>KeepAlive>StandardErrorPath>StandardOutPath></plist>launchctl load com.lcz.ssh-agent.plist #加载一个配置文件,Jobs立即启动launchctl list |grep ssh #list all of the jobs loaded into launchd ilaunchctl unload com.lcz.ssh-agent.plist参考文档

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

告诉自己,我这次失败了,重新开始吧!下次我会吸取教训,不让自己犯同样的错误的

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

相关文章:

你感兴趣的文章:

标签云: