Linux run on load

How can I run a command after boot?

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?

All Linux distributions already sets the time. Maybe the battery to your RTC on the motherboard is low?

@JoachimPileborg actually there is no RTC on my motherboard 🙂 It’s a embedded system board without RTC.

I beg to differ, @davidgo. The references you are quoting are a tad obsolete: while crontab does work, /etc/rc.local does not on systemd distros like Arch Linux. By referring to them, we might be sending Demetri on a wild goose chase. Some updating, at times, needs to be done.

3 Answers 3

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,

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

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.

Can 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)

Читайте также:  Посмотреть свой ipv6 linux

You 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.

maybe this is an unknown alias for me but @boot does not exist, just @reboot debianhelp.co.uk/crontab.htm

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.

Источник

How to start program at Linux boot automatically

Linux startup consists of several stages, during which you can configure a program to start automatically. This can be a single command, a sequence of commands, or an executable shell script. However, startup procedures may vary between different Linux distributions and versions.

Modern Linux systems boot into systemd, while older Linux versions utilize System V init. Regardless, both systems will execute cron and rc.local before loading the desktop environment, such as GNOME or KDE. In contrast, server-based Linux distributions do not load a desktop environment and instead provide a login prompt at the console. After logging in, the default shell like Bash runs.

Methods to automatically run program on Linux startup:

Running a program automatically on Linux startup via systemd

systemd is the standard system and service manager in modern Linux, responsible for executing and managing programs during startup, among other tasks. Compatible programs will include service unit files used by systemd to manage the program’s execution.

Читайте также:  Установить гугл хром кали линукс

To configure systemd to run a program automatically during Linux startup, follow these steps:

$ sudo systemctl list-unit-files --type=service [sudo] password for user: UNIT FILE STATE accounts-daemon.service enabled apparmor.service enabled apport-autoreport.service static apport-forward@.service static apport.service generated apt-daily-upgrade.service static apt-daily.service static atd.service enabled autovt@.service enabled blk-availability.service enabled bootlogd.service masked bootlogs.service masked bootmisc.service masked checkfs.service masked checkroot-bootclean.service masked checkroot.service masked cloud-config.service enabled cloud-final.service enabled cloud-init-local.service enabled cloud-init.service enabled console-getty.service disabled ##### snipped #####

You’ll have to create your own service unit if it’s a custom program or if your program doesn’t come with one during installation
Related: Creating and modifying systemd unit files

$ sudo systemctl is-enabled mysql disabled
$ sudo systemctl enable mysql Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable mysql Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
$ sudo systemctl is-enabled mysql enabled

Running a program automatically on Linux startup via cron

cron is a daemon used to execute scheduled commands stored in the cron job table (crontab), which is unique for each user in the system. It starts during system boot, either via systemd or System V init, and you can schedule your program to run during system boot by following these steps:

You’re required to select an editor for the crontab if this is the first time the user uses the command.

$ crontab -e no crontab for user - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/nano 

A crontab will be created for the user running the command and will be executed using the privileges of the user. If you need your program to run as the root user, run crontab -e as the root user itself.

# m h dom mon dow command @reboot
@reboot /sbin/ip addr | grep inet\ | tail -n1 | awk '< print $2 >' > /etc/issue && echo "" >> /etc/issue
$ crontab -e crontab: installing new crontab $
$ crontab -l # m h dom mon dow command @reboot /sbin/ip addr | grep inet\ | tail -n1 | awk '< print $2 >' > /etc/issue && echo "" >> /etc/issue

Running a program automatically on Linux startup via rc.local

rc.local is a legacy script from the System V init system, executed before displaying a login screen for the desktop environment or terminal login prompt. It is typically a Bash shell script capable of running any commands.

To configure your rc.local script, follow these steps:

As the root user, open or create the /etc/rc.local file using your preferred editor if it doesn't exist.

#!/bin/bash /sbin/ip addr | grep inet\ | tail -n1 | awk '< print $2 >' > /etc/issue echo "" >> /etc/issue exit 0

Running a program automatically on GNOME startup

GNOME is the default desktop environment for Linux distributions like Ubuntu and Red Hat. You can configure GNOME to run programs upon user login by following the instructions in the linked article:

Running a program automatically on KDE startup

KDE is another popular Linux desktop environment and the default for Kubuntu and openSUSE. It can also be configured to run programs when a user logs in, as detailed in the related article:

Running a program automatically on new Bash session

A new shell program will be spawned when you start your terminal session. Bash is the default shell for most Linux distributions, and when started, it will look for the following files in the particular starting a terminal session, a new shell program will be spawned. Bash is the default shell for most Linux distributions and, when initiated, looks for and executes the following files in order:

/etc/profile ~/.bash_profile ~/.bash_login ~/.profile

These files contain commands and logic for setting up environment variables and running required programs in the Bash language. They are also typically configured to execute other files, such as /etc/bashrc, /etc/bash.bashrc, and ~/.bashrc.

You can edit any of these files to run your program when a Bash session is started. Below is a part of a typical ~/.bashrc file:

PS1='$\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \$ ' PATH=/home/user/bin:$PATH export EDITOR=/usr/bin/vim alias ll="ls -l"

Author: Mohd Shakir Zakaria
Mohd Shakir Zakaria, a proficient cloud architect, is deeply rooted in development, entrepreneurship, and open-source advocacy. As the founder of Simplified Guide, he combines these passions to help others navigate the intricate world of computing. His expertise simplifies complex tech concepts, making them accessible to everyone. Discuss the article:

Comment anonymously. Login not required.

Источник

Оцените статью
Adblock
detector