Running programs on Linux boot up

科技动态 2012-09-29

The other day I needed to configure a Linux VM to run a few programs at system startup. It turned out that there is no single way to accomplish that that would work across all major Linux distros and Unix flavors.

First of all, while the implementation of the system startup daemon in most Linux distributions used to be compatible with the good old System V init
, BSD and derivatives always did it differently. Then, the init daemon has numerous shortcomings, but even the major Linux vendors could not have agreed on a single replacement, and some of them elected to devise their own. While Fedora, openSUSE, Mandriva and a bunch of others have been switching over to a rel=’external’ href=”http://freedesktop.org/wiki/Software/systemd/” target=”_blank”>systemd , Ubuntu has created Upstart
, Arch Linux adopted BSD-style init, and Gentoo always had a custom implementation with named runlevels. And for Mac OS X Apple had created SystemStarter, only to replace it with launchd
in Mac OS X 10.4.

However, the programs in question are not daemons. They just need to perform a certain task and terminate, and they better do that at the very end of the boot process, or at least after networking starts. The collective wisdom of the Internet says that I could simply add the respective commands to the /etc/rc.local
script that runs “at the end of each mutli-user runlevel”. Needless to say, FreeBSD and company do not have runlevels, but at least there is /etc/rc.local
, which was heavily used in the past, but now remains mostly for backward compatibility. But (surprise!) on Mac OS X that file is called /etc/rc.common
. Arrrgh!

Hope is not lost though. It turned out that there is one place on most systems and that place is even within the reach of an ordinary (i.e. unprivileged) user – crontab files
!

Just run crontab -e
and use the special string @reboot
in place of the time and date fields:

@reboot command

System crontabs often have an extra field for the account username:

@reboot user command

Important:
You better specify the full path to the command or, if you have many cron jobs, put a PATH
line at the top of your crontab file. Also, make sure to end the last line in your crontab file with a newline character or it won’t work! For more cron troubleshooting tips, refer to this collection
on AskUbuntu.

The only caveat to this solution is that on most systems cron
cannot distinguish between a system reboot and a mere restart of the cron
daemon itself. But a restart is very unlikely to occur, except when you are debugging your crontabs.


Specifically on Debian and derivatives, cron
is
capable of identifying restarts by creating a file crond.reboot in the /var/run directory. That directory is cleared early on system boot, so if the file does not exist, cron
knows it starts for the first time after a system bootstrap and runs the @reboot
tasks.

Tags:crontab,init,linux,startup


This entry was posted on Saturday, September 29th, 2012 at 2:13 pm and is filed underNotes for myself. You can follow any responses to this entry through theRSS 2.0feed. You can skip to the end and leave a response. Pinging is currently not allowed.

责编内容by:Dmitry Leskov (源链)。感谢您的支持!

您可能感兴趣的

『中级篇』容器的端口映射(28) 前几次讲了bridge 和bridge network,今天这次继续说docker的网络,docker提供一个web服务,如何让你的容器的web服务供外边的公网...
自动化软件之Pexpect 最近发现做了很多模拟,但是一些常规分析每次都需要进行查阅,套路化计算,我在想为何不能自动化每次模拟完自动执行一下呢?之前写了一个shell脚本的,现在看起来非...
两个linux shell小技巧,助你提高敲命令效率... 技巧一: sudo !! 在linux命令行敲命令时,你可能会碰到如下的情况:敲了一长串命令,潇洒的一回车,结果...
从零开始学习VoltDB 1.什么是VoltDB? 是一个优化吞吐率的高性能集群开源SQLRDBMS(Database Management System),它是一个内存关系型数据库...
HackTheBox Node Walkthrough Overview Node is a machine focused around some of the newer technologies being...