Linux run command on start

How to run a Linux Program on Startup

Here are the steps to have a program or script start on boot on a linux machine using Systemctl. I’m currently using this start several services on my raspberry pi. DigitalOcean wrote an article that goes into more detail on Systemctl.

  1. Run this command
  2. Paste in the command below. Press ctrl + x then y to save and exit

Enable the service

  • Start the service
  • Check the status of your service
  • Reboot your device and the program/script should be running. If it crashes it will attempt to restart
  • Share this:

    42 thoughts to “How to run a Linux Program on Startup”

    Thanks for this Tim – your articles helped me getting homebridge working perfectly! I was so lost without these articles – really appreciate it.

    I’m trying to take these learnings and get the alexa > sonos node.js service running explained here

    They give not so detailed instructions on how to get it up and running..

    Optional: You probably want to set this up to auto-start in the event it dies. Auto-launch is one of many solutions to make that happen. – https://www.npmjs.com/package/auto-launch

    As you may guess i’m completely lost!

    This is what I created – assuming I usually go to the folder home/pi/node_modules/sonos-http-api/
    then just type “npm start” and it starts..

    Description=Sonos Alexa Node Server

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=home/pi/node_modules/sonos-http-api/npm start
    Restart=on-failure
    RestartSec=10
    KillMode=process

    Thanks glad they have helped! I think you would want your command to first change to the directory then run npm start.

    You could also try npm start –prefix path/to/your/app

    Thanks for this article Tim.
    Sorry tehat I bother you.
    I’m making a service unit file that starts a Firefox script on boot. (I’m using Ubuntu)
    I’m need your help, please contact me to fix this unit.

    Opening web browser on boot

    I thought I might be able to get this done by making a service unit file that starts a firefox script on boot.

    Description=”Run firefox at boot”
    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple ExecStart=/usr/bin/google (“#!/bin/bash su ‘username’ -c /usr/bin/firefox-stable)
    Restart=on-failure
    RestartSec=10
    KillMode=process

    I don’t think you need the /usr/bin/google part

    What command is this? No one says what this command is supposed to be. Am I supposed to just “know” what goes there?

    YOUR_COMMAND_HERE can be whatever command you want to run on startup

    Thank you for this straight-forward article – it’s exactly what I’ve been searching for. I’ve written a daemon process in C but was having trouble with other complicated instructions in getting it to run as a system process. This worked first time!
    I hope you can understand the simple delight of an engineer of some thirty years experience on seeing a System OK LED start flashing on boot!

    Best regards,
    Peter

    That’s great to hear!! Glad it helped

    When i try to boot .net core app, it’s not working fully, any other way?

    Are you seeing any errors?

    This article has made a local hero at my place of work. Thanks a lot.

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/root/EAP-7.1.0/bin/standalone.sh
    Restart=on-failure
    RestartSec=10
    KillMode=process

    That’s awesome! Glad it helped.

    Hi Tim,
    I tried your method on my application program on Debian OS. It is not working for me.
    My program is a GUI based application program, which is located in the folder of /home/linaro/. I run my application from Terminal is as below:
    ./my_app
    It works fine.

    Читайте также:  Adobe premiere windows 10 синий экран

    Below is the service file my_app.service, which is stored in the folder of /ect/ststemd/system/
    Description=My_main_app

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/home/linaro/my_app
    Restart=on-failure
    RestartSec=10
    KillMode=process

    It failed on start the service.
    The systemctl log is as below:
    [/etc/system/system/my_app.service:1] Assignment outside of section. Ignoring.
    [/etc/system/system/my_app.service:3] Assignment outside of section. Ignoring.
    [/etc/system/system/my_app.service:4] Assignment outside of section. Ignoring.
    [/etc/system/system/my_app.service:6] Unknown section ‘service’. Ignoring.
    my_app.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.

    Would you please let me know what’s wrong with it?
    Thanks.

    Is there a formatting issue with the service file?

    I checked again. I can’t find out any formatting issue.

    Unfortunetly, this doesn’t work for me. I have the following service setup:
    Description=xbindkeys packege

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/usr/bin/xbindkeys
    Restart=on-failure
    RestartSec=10
    KillMode=process

    But at boot nothing happens.

    Do you see any errors?

    Hi Tim,
    so if my Server.js file is in home/ubuntu/backend/Server.js my EXECSTART should be home/ubuntu/backend/node Server.js?

    Is home your user home folder? May need to be the full path.

    Hi Tim,
    i want to run a startup code for ZED board how could i do because in zed board there is no system and bashrc

    No sure about a ZED board. What operating system is I running ?

    Hi Tim – Been trying this method to get HomeBridge to launch on start vs. your prior post. I keep getting an error message when I start the service:

    $ sudo systemctl start home
    Failed to start home.service: Unit home.service is not loaded properly: Invalid argument.
    See system logs and ‘systemctl status home.service’ for details.

    When I go into the Log this is what I see:
    ● home.service
    Loaded: error (Reason: Invalid argument)
    Active: inactive (dead)

    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:1] Assignment outside of section. Ignoring.
    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:3] Assignment outside of section. Ignoring.
    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:4] Assignment outside of section. Ignoring.
    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:8] Executable path is not absolute, ignoring: homebridge
    Jun 17 08:37:15 raspberrypi systemd[1]: home.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.

    The command is as follows:

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=homebridge
    Restart=on-failure
    RestartSec=10
    KillMode=process

    Hey John, try adding the full path to the homebridge command. You should be able to use whereis homebridge

    Thanks. That sort of worked… It launches Homebridge on start. But it seems like its launches a different instance / config file than I setup in my user (pi). Do I fix that by changing the revised path (/usr/bin/homebridge)?

    Yes that should fix it

    Sorry for the stupid questions, but this is all new to me. If the user is “pi”, how would I update the path? the path “/usr/bin/homebridge” launched a version of Homebridge that didn’t have my config file.

    Hello Tim, I tried setting up pCloud on Ubuntu 16.04 with the walkthrough a few days ago, but something’s wrong and I don’t understand what it’s trying to tell me. Just to be clear, it has not been able to launch pCloud once since I set it up like this. I just didn’t find the time to post about it here. “ultra” is the name of my system.

    “`
    $ systemctl status pcloud

    ● pcloud.service
    Loaded: loaded (/etc/systemd/system/pcloud.service; enabled; vendor preset: enabled)
    Active: activating (auto-restart) (Result: exit-code) since Sam 2019-06-22 11:48:12 CEST; 1s ago
    Process: 8534 ExecStart=/home/thymaro/opt/pcloud/pcloud (code=exited, status=1/FAILURE)
    Main PID: 8534 (code=exited, status=1/FAILURE)
    CGroup: /system.slice/pcloud.service

    Jun 22 11:48:12 ultra systemd[1]: pcloud.service: Unit entered failed state.
    Jun 22 11:48:12 ultra systemd[1]: pcloud.service: Failed with result ‘exit-code’.

    First, how do you format code in this reply box?

    Maybe this helps identifying the problem:

    *************************************
    Description=Starts the pCloud service on startup.

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/home/thymaro/opt/pcloud/pcloud
    Restart=on-failure
    RestartSec=10
    KillMode=process

    @TIM,
    i’m using a linux as well.

    Читайте также:  Активность жесткого диска 100 windows 10

    I’m new to linux, python3 & Raspberry Pi. My forte is Windows & Visual Basic. I’m
    having problems setting up a service for the following.

    “/home/pi/test” – This is the script I want to run after the Raspberry Pi boots.
    It ensures the current directory is ‘/home/pi’ (for system data logging), then
    starts a terminal and runs a python3 program that monitors home system status
    (Network & AC power). The scripts are executable and work from a terminal and
    the GUI, and I’ve tried it both ways below:

    #!/bin/sh
    #
    chdir /home/pi
    /usr/bin/x-terminal-emulator -e /usr/bin/python3 /home/pi/HomeStat.py

    #!/bin/sh
    #
    cd /home/pi
    x-terminal-emulator -e python3 HomeStat.py

    “HomeStat.service” – The following is the service I set up per your instructions

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/home/pi/test
    Restart=on-failure
    RestartSec=10
    KillMode=process

    This is the status message I received

    ● HomeStat.service
    Loaded: loaded (/etc/systemd/system/HomeStat.service; enabled; vendor preset: enabled)
    Active: activating (auto-restart) (Result: exit-code) since Thu 2019-07-18 19:18:23 EDT; 9s ago
    Process: 1587 ExecStart=/home/pi/test (code=exited, status=1/FAILURE)
    Main PID: 1587 (code=exited, status=1/FAILURE)

    Jul 18 19:18:23 raspberrypi systemd[1]: HomeStat.service: Unit entered failed state.
    Jul 18 19:18:23 raspberrypi systemd[1]: HomeStat.service: Failed with result ‘exit-code’.

    I don’t understand what the status message is saying! Where do I find out information on these
    status messages? What is status=1/FAILURE? Is there a listing of these error
    codes with a breakdown or synopsis of the failure. Any help would be appreciated.

    Does chdir work? I believe that is a Windows command to change directories.

    Источник

    How to Run a Command on Startup in Linux

    One of Linux’s strengths as an operating system is its ability to be configured and tweaked to suit your tastes. Using the command line allows you to issue powerful commands that are unavailable to other OS users, and use of the command line is almost a necessity to get the most out of your Linux distribution. For example, sometimes you may want to run a script or a command whenever you start your Linux machine. There is more than one way to do this.

    Put the command in your crontab file. The crontab file in Linux is a daemon that performs user-edited tasks at specific times and events. To edit the file, open a terminal and type «sudo crontab -e» to open your crontab file in the default text editor. At the first available line, type «@reboot xxxx», where «xxxx» is the command you wish to run. Save the file and exit.

    Put a script containing the command in your /etc directory. Create a script such as «startup.sh» using your favorite text editor. Save the file in your /etc/init.d/ directory. Change the permissions of the script (to make it executable) by typing «chmod +x /etc/init.d/mystartup.sh».

    Edit the /rc.local script using your text editor. On Fedora systems, this script is located in /etc/rc.d/rc.local, and in Ubuntu, it is located in /etc/rc.local. Once you add the commands you wish to run — making sure you do so as root — save the file and exit. The commands will run after the next startup.

    Wolfram Donat is an information technology professional and writer currently finishing a degree in computer systems engineering. He has had success writing for both on- and offline audiences, and moonlights as a professional copywriter as studies permit. He is currently shopping his first novel to agents and publishers, and is hard at work on his second.

    Источник

    How to automatically run program on Linux startup

    There are a few stages of Linux startup, and you can set your own autostart script or program at any of the stages where it could be a single command, a chain of commands, or an executable shell script. There could be, however, some differences in startup procedure between different Linux distributions and versions.

    Читайте также:  Образ загрузчика для mac os

    Modern Linux will first boot into systemd while older versions of Linux uses System V init. Both methods will run cron and rc.local before the desktop environment such as GNOME or KDE is loaded. On the other hand, server-based Linux distributions will run the default shell such as Bash after the user logged in at the console instead of loading the desktop environment.

    Methods to automatically run program on Linux startup:

    Automatically run program on Linux startup via systemd

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

    You can configure systemd to run programs automatically during Linux startup following these steps:

    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

    enabled service unit is executed during boot

    Automatically run program on Linux startup via cron

    cron is a daemon to execute scheduled commands. The commands are stored in the cron job table or crontab and are unique for each user in the system. It’s started during system boot either by systemd or System V init, and you can schedule your job or program to be executed right during the system boot itself 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.

    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.

    @reboot defines the job to be executed during system boot.

    Use full path for your programs when possible and write your commands in a single line.

    The file is saved in /var/spool/crontab/

    Automatically run program on Linux startup via rc.local

    rc.local is a legacy from the System V init system. It is the last script to be executed before proceeding to a login screen for the desktop environment or a login prompt at the terminal. It’s usually a Bash shell script, and you can run anything from the script.

    You can configure your rc.local script following these steps:

    It must start with interpreter (/bin/bash) and ends with an exit code (0 is for success)

    The file will be executed as the root user during system boot

    Automatically run program on GNOME startup

    GNOME is the default desktop environment for Linux distributions such as Ubuntu and Red Hat. GNOME can be configured to run programs when a user logs in and can be configured by following the below article:

    Automatically run program on KDE startup

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

    Automatically run program on new Bash session

    A new shell program will be spawned when you start your terminal session. Bash is the default shell for most Linuxdistributions, and when started, it will look for the following files in the particular order and executes them.

    These files contains commands and logics to set up proper environment variables and run required programs in Bash language. It’s also configured to normally execute other files such as /etc/bashrc, /etc/bash.bashrc and

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

    Comment anonymously. Login not required.

    Источник

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