How can I run a command after boot?
Solution 1
Depends on distros.
If you are on Debian-derived distros, there are at least two proper places, /etc/rc.local and crontab. You may invoke crontab as root,
crontab -e
and then insert this line
@reboot /home/my_name/bin/my_command
where my_command
is an executable file (chmod 755 my_command
).
Alternatively, you may place a line like this
/home/my_name/bin/my_command
in /etc/rc.local, and this will be executed last. In any case, pls do remember you are using root environment, not yours. Also for this reason, it is best to use absolute paths.
If you are on a systemd distro (Arch Linux, Fedora 19,...) the first option (crontab) is still valid, while the second one (/etc/rc.local) does not exist any longer. In this case, you should learn how to start a service to be run by systemctl
, but this may be more than you bargained for with your simple question.
Solution 2
If your system is running a version of cron
that supports it (specifically Vixie cron), you can use @reboot
in a cron job.
This is one of 8 special strings that it supports.
Quoting the crontab(5)
man page (from my Ubuntu 12.04 system):
Instead of the first five fields, one of eight special strings may appear:
string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".
Please note that startup, as far as
@reboot
is concerned, is the time when the cron(8) daemon startup. In particular, it may be before some system daemons, or other facilities, were startup. This is due to the boot order sequence of the machine.
This is far from being the only way to run something at boot time, but it's an alternative.
Solution 3
Pretty much all variants of Linux (going back a long, long time) have a file /etc/rc.local which runs on startup - you can just add the command to it.
That said, you don't even need to do that. The "correct" way would be to modify / add the line kernel.sysrq = 1 into /etc/sysctl.conf
Related videos on Youtube
Demi
Updated on September 18, 2022Comments
-
Demi over 1 year
I would like to run the simple shell command
echo 1 > /proc/sys/kernel/sysrq
at each boot, to enable the sysrq keys. When in boot should this be done, and where should I put a script to do it?-
Some programmer dude over 11 yearsAll Linux distributions already sets the time. Maybe the battery to your RTC on the motherboard is low?
-
Admin over 11 years@JoachimPileborg actually there is no RTC on my motherboard :) It's a embedded system board without RTC.
-
-
davidgo over 10 yearsCan you confirm this for Fedora ? My searching tells me that rc.local will still run if it exists and is executable, but is not set up by default. I perceived troubling ramifications if rc.local does not run ! (I could only find references for this for Fedora 18 - forums.fedoraforum.org/showthread.php?t=291889)
-
MariusMatutiae over 10 yearsYou are right, in Fedora 19 you might still have it, if you really want it. But it is no longer packaged, docs.fedoraproject.org/en-US/Fedora/16/html/Release_Notes/… bullet 3.2.4. Also, it does not exist in Arch Linux altogether, so I thought it safe to include a warning about that.
-
Zarathustra over 9 yearsmaybe this is an unknown alias for me but
@boot
does not exist, just@reboot
debianhelp.co.uk/crontab.htm -
MariusMatutiae almost 7 yearsHow can this answer have been written more than a year before the original question?
-
Keith Thompson almost 7 years@MariusMatutiae meta.stackoverflow.com/q/294543/827263
-
Javier Arias almost 7 years+1 for specifying that requires Vixie cron.
-
shrimpwagon over 3 yearsThis has never ever worked for me with any Debian distro
-
Kalib Zen about 3 yearsI almost got what you said but I'm stuck with your last sentence: The "correct" way would be to modify / add the line kernel.sysrq = 1 into /etc/sysctl.conf (you stopped here) ... do you have extra explanation here? then how can I run the extra command after putting
kernel.sysrq=1
-
davidgo about 3 years@KalibZen You can type "sysctl -p" to get sysctl to reload the settings from /etc/sysctl.conf.
-
Kalib Zen about 3 yearsNo, not that one, the question is about startup.. The first paragraph I got it you talk about /etc/rc.local but the last paragraph I don't understand what are you trying to say. What is the kernel.sysrq =1 has to do with startup ?
-
Kalib Zen about 3 yearsOk, now I got it, the last paragraph is not about startup as mentioned by OP but you gave solution how to overcome that without using startup method. Forgive me to just read the title question not the content of the question. But if you could rephrase your last paragraph to explicitly say, 'for your case' then it's going to be clear. I mistakenly read the last paragraph as 'you don't even need to do that; meaning I misunderstood not to do the rc.local thing and you came out with the kernel.sysrq=1 thing)
-
davidgo about 3 yearsNot sure what you are saying. (The OS automatically reads sysctl.conf and actions this on startup)