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.
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.
Something like Cron?
Note the @reboot
entry
This is the most flexible approach, and the one most like Windows' "Scheduled Tasks" (better actually).
/etc/cron.d
, or call crontab from your program without them knowing.
–
Aug 16 '11 at 15:07
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).
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):
User=myuser
Restart=on-failure|on-watchdog|on-abnormal|always
Type=simple|forking|oneshot|notify|dbus
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 tosystemd
: 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:
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.
cron
scheduler is your friend.
–
Aug 16 '11 at 15:02
Startup applications
using home buttonAdd
Startup command box
enter the command OK
(You should see your new command in the list) Close
Test by restarting or logging out and back in.
Source: https://help.ubuntu.com/community/AddingProgramToSessionStartup
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.
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.