Running script on startup linux

How to Run Script or Program at Startup with Systemd on Linux

There are plenty of ways to automatically run a script or command at Linux boot up and after reboot, either by using cron , rc.local or my preferred method — systemd . It creates services which are easier to manage. You can start/stop/enable/disable them with systemctl after they’re set up. Furthermore, services can even restart themselves after startup failure or crash.

Create Service

Create the unit configuration file /etc/systemd/system/your-service.service with the text editor of your choice:

    Vim (command-line editor for advanced user)

$ sudo vim /etc/systemd/system/your-service.service 
$ sudo nano /etc/systemd/system/your-service.service 
$ sudo touch /etc/systemd/system/your-service.service $ gedit admin:/etc/systemd/system/your-service.service 

Then add the following lines to your-service.service :

/etc/systemd/system/your-service.service[Unit] Description=YourService [Service] ExecStart=/path/to/your/script.sh [Install] WantedBy=multi-user.target Plain text

Service example

Auto Restart (Optional)

To tell the service to restart itself after failure, append Restart=always under the Service section:

/etc/systemd/system/your-service.service[Service] . Restart=always Plain text

Acceptable values of the Restart directive include no , on-success , on-failure , on-abnormal , on-watchdog , on-abort and always .

It’s also a good idea to set RestartSec to configure the amount of time to sleep before restarting the service.

/etc/systemd/system/your-service.service[Service] . RestartSec=5 Plain text

Set Environment (Optional)

To set the environment variables:

/etc/systemd/system/your-service.service[Service] . Environment="production=true" Plain text

Set Working Directory (Optional)

/etc/systemd/system/your-service.service[Service] . WorkingDirectory=/home/user Plain text

Enable Service

Before enabling your service, change the permissions of your-service.service to 644 :

$ sudo chmod 644 /etc/systemd/system/your-service.service 

Remember to reload the daemon after the systemd unit files are changed:

$ sudo systemctl daemon-reload 

Then enable the service so it will run at system startup:

$ sudo systemctl enable your-service 

Verify It’s Working

To verify the service is working, start the service and check its status:

$ sudo systemctl start your-service $ systemctl status your-service 
○ greet.service - Greet Loaded: loaded (/etc/systemd/system/greet.service; enabled; vendor preset: disabled) Active: inactive (dead) since Thu 2022-02-17 15:12:19 CST; 3s ago Process: 7810 ExecStart=/greet.sh (code=exited, status=0/SUCCESS) Main PID: 7810 (code=exited, status=0/SUCCESS) CPU: 3ms Feb 17 15:12:19 linux systemd[1]: Started Greet. Feb 17 15:12:19 linux greet.sh[7810]: Hello World Feb 17 15:12:19 linux systemd[1]: greet.service: Deactivated successfully. Output

Since mine is a simple echo «Hello World» script, it’ll be considered as inactive once executed.

Disable Service

To disable the service if you don’t want it to run at system startup anymore:

$ sudo systemctl diable your-service 

Stop Immediately

Disabled services can still be running in the background, to stop the service immediately:

$ sudo systemctl stop your-service 

Comments

Preview of Configure PPPoE DSL Connection on Arch Linux/Manjaro with pppoe-setup

Configure PPPoE DSL Connection on Arch Linux/Manjaro with pppoe-setup

Most ISPs (Internet Service Providers) offer multiple dynamic IP addresses and even come with a static one. To take advantage of that, we’ll be setting up a PPPoE (Point-to-Point Protocol over Ethernet) DSL (Digital Subscriber Line) connection on our own machine to obtain an independent IP address.

Читайте также:  Realtek high definition audio driver linux

Preview of Two Ways to Install Packages from AUR on Arch Linux/Manjaro

Two Ways to Install Packages from AUR on Arch Linux/Manjaro

Just like Debian/Ubuntu’s package manager apt, Arch Linux/Manjaro has its own package manager called pacman to help you install packages. But the story doesn’t end there. On Arch-based Linux you can even install community-maintained packages from AUR (Arch User Repository), which is a lot like PPA (Personal Package Archive) of Ubuntu.

Preview of Auto-Renew Let's Encrypt Free Certificates for Nginx on Linux

Auto-Renew Let’s Encrypt Free Certificates for Nginx on Linux

More than 75% of all websites use HTTPS nowadays. Serving content over HTTPS is not only good practice for search engine optimization (SEO), but essential for security reasons, especially for those requests containing sensitive information.

Preview of Create/Remove/Resize Swap Space without Modifying Partitions on Linux

Create/Remove/Resize Swap Space without Modifying Partitions on Linux

When a Linux machine runs out of physical memory (a.k.a random-access memory — RAM), the swap space will be used instead. It literally swaps data from your RAM to your disk space to free up memory. In some perspectives, it even speeds up your CPU too, since when the less the memory is free, more time the CPU spend on GC (Garbage Collection).

Источник

How do I run a script at start up? [duplicate]

I need this script to run every time the system starts (even if no one logs in to the system). What do I need to do in order to make this happen?

5 Answers 5

You will need root privileges for any the following. To get root, open a terminal and run the command

and the command prompt will change to ‘#’ indicating that the terminal session has root privileges.

Alternative #1: Add commands to /etc/rc.local

with content like the following:

# This script is executed at the end of each multiuser runlevel /path/to/my/script.sh || exit 1 # Added by me exit 0 

Alternative #2: Add an Upstart job (for systems older than 15.04) (not recommended)

with content like the following

description "my job" start on startup task exec /path/to/my/script.sh 

Official statement from upstart website -> «Project is in maintaince mode only. No new features are being developed and the general advice would be to move over to another minimal init system or systemd.»

Читайте также:  Centos linux отличие от centos stream

Alternative #3: Add an init script (obsolete)

Create a new script in /etc/init.d/myscript .

(Obviously it doesn’t have to be called «myscript».) In this script, do whatever you want to do. Perhaps just run the script you mentioned.

chmod ugo+x /etc/init.d/myscript 

Configure the init system to run this script at startup.

update-rc.d myscript defaults 

It is possible to do the same thing by writing a short Upstart job. Initscripts are still supported, however, and are easy to use.

You don’t need root, or to even login.

You can edit your crontab ( crontab -e ) and create an entry like this:

This way, you can run it as a regular user. @reboot just means it’s run when the computer starts up (not necessarily just when it’s rebooted).

P.S.: Regarding comments that this does not work properly

Some have said that this doesn’t work on Debian-based distros, such as Ubuntu. I have personally successfully used this method on both Ubuntu and Mint. There are a few things to consider, however.

The @reboot jobs will run when the cron daemon starts. I’ve found that on Debian-based distros, this may occur before the /home partition has been mounted. If the script you’re running is in your home folder, it will fail.

Additionally, this isn’t limited to Debian-based distros, but if your home folder is encrypted, it may not be decrypted until after you login. There is probably no way around this.

Also, your network interface may not be up yet, and if the command requires Internet access, it may fail.

Finally, again, this is not limited to Debian-based distros, but cron runs under a much more limited environment than your shell runs under. In particular, the PATH variable has much fewer paths. It is possible that the command being run isn’t found, if it’s in, for example, something like $HOME/.local/bin , which may be in your PATH in your shell session, but not under cron . It’s even possible that the command being run depends on some environment variable that’s not set in cron .

Читайте также:  Установка вине на линукс

So, there are a number of reasons why your command will to run under cron, but it’s not because @reboot doesn’t work on your distro.

Источник

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