16

Is there any way to add an application/script to the Linux startup so every time the system boots it will be executed?

I am looking for some automated way, i.e. the user should not add this by cron job or something like that.

migrated from stackoverflow.com Aug 29 '11 at 2:08

This question came from our site for professional and enthusiast programmers.

11

Something like Cron?

Note the @reboot entry

This is the most flexible approach, and the one most like Windows' "Scheduled Tasks" (better actually).

  • is it possible to add a job in CRON without user's intention ? – Sourav Aug 16 '11 at 15:01
  • yes. just write to /etc/cron.d, or call crontab from your program without them knowing. – awoodland Aug 16 '11 at 15:07
  • What do you mean? If you mean manually add one programatically, then yes. Crontabs are just text files. – kevlar1818 Aug 16 '11 at 15:07
10

Xorg auto-start

Apart from system-level startup scripts your desktop environment might have its own way of auto-running programs. The folder .config/autostart is supposed to be a desktop-neutral way of defining autorun entries. /etc/xdg/autostart is for system-wide configuration. Details about the spec at http://developer.gnome.org/autostart-spec/.

For LXDE autostart entries can also be set in ~/.config/lxsession/LXDE/autostart.

It is a bit different if you need to run your scripts after the network is up and running. In that case you should check the special post-connect scripts that can be defined for your network manager. Both NetworkManager and wicd have their own ways of specifying post-connect autorun entries. If the network is configured via ifupdown, then post-up scripts can be placed in the /etc/network/if-up.d/ folder. But a better approach to running post-connect scripts might be systemd (for systems that support it, which is the majority of modern distros).

Autostart as a systemd service

If the thing you want to autostart is not a graphical app which requires a desktop then it's best to avoid using any autostart facilities provided by xorg or by your current desktop environment.

systemd has become ubiquitous in many modern distros, and it offers a lot of control and flexibility in terms of how your services are started and how they run.

I'll summarize some benefits (systemd can do a lot more):

  • Run as root or as specific user: e.g. User=myuser
  • Restart services on failure with configurable timeouts: Restart=on-failure|on-watchdog|on-abnormal|always
  • Setting the service type: Type=simple|forking|oneshot|notify|dbus
  • Establish startup preconditions and dependencies, i.e. you can set your service to start after the network is up (Wants=network-online.target in the [Unit] section).

An example service that starts a telegram-cli daemon. Place it in /etc/systemd/system/tg.service.

[Unit]
Description=MyDaemon
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/telegram-cli -k /etc/telegram-cli/tg-server.pub -W -P 1234 -d -vvvRC
ExecStop=/usr/bin/pkill -f telegram-cli

User=jicu

[Install]
WantedBy=multi-user.target

Now you can enable the service to autostart:

sudo systemctl enable tg

Start the service:

sudo systemctl start tg

Stop the service:

sudo systemctl stop tg

Check the status:

systemctl status tg

Disable the service:

sudo systemctl disable tg

To save you extra typing you can add in your ~/.bashrc the line alias sc='sudo systemctl $*' then you'll be able to shorten the commands above to e.g. sc start tg.

NOTE: If you've used cron then are aware that crontab entries are run in a restricted environment — the same applies to systemd: always use absolute paths, and make no assumptions of any variables being defined. Explicitly set any variables that your scripts depend on. systemd will not use your user's .bashrc and $PATH.

More info:

7

Yes it is possible to run programs at startup on Linux by defining the paths to executables in rc.local that either resides in the /etc or /etc/rc.d directory, e.g.:

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/path/to/executable

Note: do not forget to assign executable rights as described in the documentation of the file, i.e. Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure that this script will be executed during boot.

  • adding entry in the file will start the program each time ? – Sourav Aug 16 '11 at 15:01
  • 2
    Each time you reboot, yes. If you want regularly scheduled jobs (every day, hour, week, or any odd combination) then the cron scheduler is your friend. – Dirk Eddelbuettel Aug 16 '11 at 15:02
2
  1. Search for Startup applications using home button
  2. Click Add
  3. Enter a name to call the application (any name will do)
  4. In the Startup command box enter the command
  5. Click OK (You should see your new command in the list)
  6. Click Close

Test by restarting or logging out and back in.

Source: https://help.ubuntu.com/community/AddingProgramToSessionStartup

1

Each distribution uses each own bootstrap technique, so need to look at docs for your distro. /etc/rc.local is a place where you can put some automated scripts, but it is really outdated. Now most of linux based systems uses runlevels or systemd bootstraping, so most of autostarted jobs could be fine-grade controlled.

0

I found my answer here: https://stackoverflow.com/questions/7221757/run-automatically-program-on-startup-under-linux-ubuntu I was able to create a file / script to turn off my trackpad while I in my Linux Ubuntu 12.10 session.

  • 3
    Glad you found a solution, could you include a summary of the steps in your answer, just so this answer is a more useful than just a link. Thanks! – slm Apr 24 '13 at 4:15
  • Was looking for a way to automate and turn off my trackpad at startup. The command I entered in Terminal was 'sudo modprobe -r psmouse' This would turn off the trackpad while in the current session. I wanted to automate this at boot. I did some research and wrote a script by modifying the file /etc/rc.local, adding 'sudo modprobe -r psmouse. Then issuing the following commands from Terminal: 'sudo mv /filename /etc/init.d/' 'sudo chmod +x /etc/init.d/filename' 'sudo update-rc.d filename defaults' Now the touchpad is off at startup. – CatGuyTX Apr 25 '13 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.