What is the Linux equivalent of Windows Startup?
Something like Cron?
Note the @reboot
entry
This is the most flexible approach, and the one most like Windows' "Scheduled Tasks" (better actually).
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 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:
- Understanding systemd
- systemd services
- systemd units
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.