Linux start program on boot

How can I configure a service to run at startup

but it’s not configured to auto start when the PC reboots. How can I configure it to start automatically, even if no one is logged into the PC?

@user154721 What arguments did you give update-rc.d to make autostart work? I’ve tried various options but haven’t had any luck.

Auto-start programs are configured in autostart manifests or in *.service files in several locations, as well as in init.d or crontab . See: unix.stackexchange.com/a/525845/43233

5 Answers 5

sudo update-rc.d minidlna defaults 

This should add the service to the automatic startup system. But if you get:

System start/stop links for /etc/init.d/minidlna already exist. 
sudo update-rc.d minidlna enable 

P.S.: For further detail look at the man page for update-rc.d by typing the command man update-rc.d

thanks, but what if I get this output when running that command?: System start/stop links for /etc/init.d/minidlna already exist.

if minidlna doesnt write to any logfile you won’t find it any where. Does it maybe fail because there is no network connection when it is started? Try to look at the configuration if it is possible turn on debugging and/or logging to find out whats wrong.

I am also, got the same error update-rc.d: /etc/init.d/mongod: file does not exist . Then, sudo systemctl enable mongod.service worked for me.

Since Ubuntu 15.10 and newer (resp. Debian 8 «jessie» and newer), you have to use the following command to configure your service minidlna to run at startup:

sudo systemctl enable minidlna.service 

And to disable it again from starting at boot time:

sudo systemctl disable minidlna.service 

This works with all service names available on your system. To find out available service names, just list the filenames of the service files:

ls /lib/systemd/system/*.service ls /etc/systemd/system/*.service 

As for sudo, I recieve Unknown operation enable. If I discard sudo it will ask me which user I would like to choose. Pick your root user and you will be able to set the desired value.

@tanius I have followe the procedure of adding new services to /etc/systemd/system/*.service. I would like to know why no one suggested creating or editing these files as it seems to provide mode options (Restart, RestartSec, ExecStop, etc. )

@tiagoams Creating a service file was just not the question here, since the OP states that a service minidlna already exists but does not autostart. But thanks for the pointer to service files under /etc , I added that to the answer now.

update-rc.d service_name defaults 
update-rc.d -f service_name remove 

defaults => default run levels 2,3,4 and 5

update-rc.d tomcat7 defaults 

When I do this command, I get «System start/stop links for /etc/init.d/tomcat7 already exist». however, when I reboot, it does not start tomcat, I always have to do «service tomcat7 start».

Читайте также:  Dfu util stm32 linux

in my case sudo update-rc.d myservice default always completes with no output and the service won’t start on boot-up or with sudo service myservice start which also completes silently. sudo /etc/init.d/myservice start works however

Sometimes you need to run a script on boot process, for example run an iptables config at boot process. So you don’t have to run the script manually every rebooting.

You can run your script on boot process in Ubuntu by adding it to /etc/init.d/rc.local file. Look the steps below.

    Open /etc/rc.local file with this command:

sh /home/ivan/iptables.sh echo 'Iptable Configured!' 

Comments of /etc/init.d/rc.local in Ubuntu 12.04 says «Short-Description: Run /etc/rc.local if it exist», so maybe adding the scripts to /etc/rc.local would be better idea?

Vadim, rc.local is just run at boot, nothing is achieved on shutdown. The process would most likely be killed by the OS on shutdown.

FYI: The difference between rc.local vs adding it to init, is that rc.local is executed at the end of the init startup sequence, rather than as part of it

In ubuntu version 18.04 TLS, I found that update-rc.d does not work fine if there is no specific comment block in the start script that looks like this:

### BEGIN INIT INFO # Provides: myprogram # Required-Start: $ local_fs $ remote_fs $ syslog $ network $ time # Required-Stop: $ local_fs $ remote_fs $ syslog $ network # Default-start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: myprogram some description ### END INIT INFO 

Источник

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:

Читайте также:  Linux размер tcp окна

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.

Источник

Automatically run a program on startup under Linux Ubuntu [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I'd need a program to be run every time I start up my Ubuntu Linux. So I'd need to add it to my startup programs list. Just one problem: I'd need to do it via the terminal.

@Flexo This answer is not present in neither of the links you provided. +1 to this question and answer

For a simple, portable way to do this, you can use Cron. Run crontab -e to edit your user's crontab; add @reboot command to run command on each boot.

1 Answer 1

sudo mv /filename /etc/init.d/ sudo chmod +x /etc/init.d/filename sudo update-rc.d filename defaults 

The script should now start on boot. Note that this method also works with both hard links and symbolic links ( ln ).

At this point in the boot process PATH isn't set yet, so it is critical that absolute paths are used throughout. But, as pointed out in the comments by Steve HHH, explicitly declaring the full file path ( /etc/init.d/filename ) for the update-rc.d command is not valid in most versions of Linux. Per the manpage for update-rc.d, the second parameter is a script located in /etc/init.d/* .

Also as pointed out in the comments (by Charles Brandt), /filename must be an init style script. A good template was also provided - System V init script template.

As pointed out in the comments (by Russell Yan), this works only on default mode of update-rc.d.

According to the manual of update-rc.d, it can run on two modes: "the machines using the legacy mode will have a file /etc/init.d/.legacy-bootordering ", in which case you have to pass sequence and runlevel configuration through command line arguments.

The equivalent argument set for the above example is

sudo update-rc.d filename start 20 2 3 4 5 . stop 20 0 1 6 .

Источник

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