Start command on boot linux

How do I run a single command at startup using systemd?

How can I get started? Is there a basic template I can build on? I’ve tried to use an extremely simple (file: /lib/systemd/system/spark.service ):

[Unit] Description=Spark service [Service] ExecStart=sudo ./path/to/spark/sbin/start-all.sh 

Hi @WillemK, I had looked at this page already. This issue I found is I can’t just replace exec with ExecStart= . Plus, I haven’t used upstart before.

@AndreaLazzarotto I think OP is trying to run the script the way OP would in the terminal hence the . .

3 Answers 3

Your .service file should look like this:

[Unit] Description=Spark service [Service] ExecStart=/path/to/spark/sbin/start-all.sh [Install] WantedBy=multi-user.target 

Now, take a few more steps to enable and use the .service file:

  1. Place it in /etc/systemd/system folder with a name like myfirst.service .
  2. Make sure that your script is executable with:
 chmod u+x /path/to/spark/sbin/start-all.sh 
 sudo systemctl start myfirst 
 sudo systemctl enable myfirst 
 sudo systemctl stop myfirst 

Notes

  1. You don’t need to launch Spark with sudo in your service, as the default service user is already root.
  2. Look at the links below for more systemd options.

Moreover

Now what we have above is just rudimentary, here is a complete setup for spark:

[Unit] Description=Apache Spark Master and Slave Servers After=network.target After=systemd-user-sessions.service After=network-online.target [Service] User=spark Type=forking ExecStart=/opt/spark-1.6.1-bin-hadoop2.6/sbin/start-all.sh ExecStop=/opt/spark-1.6.1-bin-hadoop2.6/sbin/stop-all.sh TimeoutSec=30 Restart=on-failure RestartSec=30 StartLimitInterval=350 StartLimitBurst=10 [Install] WantedBy=multi-user.target 
sudo systemctl start spark.service sudo systemctl stop spark.service sudo systemctl enable spark.service 

Further reading

Please read through the following links. Spark is a complex setup, so you should understand how it integrates with Ubuntu’s init service.

Источник

How can I run a command after boot?

I would like to run the simple shell command echo 1 > /proc/sys/kernel/sysrq at each boot, to enable the sysrq keys. When in boot should this be done, and where should I put a script to do it?

All Linux distributions already sets the time. Maybe the battery to your RTC on the motherboard is low?

@JoachimPileborg actually there is no RTC on my motherboard 🙂 It’s a embedded system board without RTC.

I beg to differ, @davidgo. The references you are quoting are a tad obsolete: while crontab does work, /etc/rc.local does not on systemd distros like Arch Linux. By referring to them, we might be sending Demetri on a wild goose chase. Some updating, at times, needs to be done.

3 Answers 3

If you are on Debian-derived distros, there are at least two proper places, /etc/rc.local and crontab. You may invoke crontab as root,

and then insert this line

 @reboot /home/my_name/bin/my_command 

where my_command is an executable file ( chmod 755 my_command ).

Alternatively, you may place a line like this

in /etc/rc.local, and this will be executed last. In any case, pls do remember you are using root environment, not yours. Also for this reason, it is best to use absolute paths.

If you are on a systemd distro (Arch Linux, Fedora 19. ) the first option (crontab) is still valid, while the second one (/etc/rc.local) does not exist any longer. In this case, you should learn how to start a service to be run by systemctl , but this may be more than you bargained for with your simple question.

Can you confirm this for Fedora ? My searching tells me that rc.local will still run if it exists and is executable, but is not set up by default. I perceived troubling ramifications if rc.local does not run ! (I could only find references for this for Fedora 18 — forums.fedoraforum.org/showthread.php?t=291889)

You are right, in Fedora 19 you might still have it, if you really want it. But it is no longer packaged, docs.fedoraproject.org/en-US/Fedora/16/html/Release_Notes/… bullet 3.2.4. Also, it does not exist in Arch Linux altogether, so I thought it safe to include a warning about that.

maybe this is an unknown alias for me but @boot does not exist, just @reboot debianhelp.co.uk/crontab.htm

If your system is running a version of cron that supports it (specifically Vixie cron), you can use @reboot in a cron job.

This is one of 8 special strings that it supports.

Quoting the crontab(5) man page (from my Ubuntu 12.04 system):

Instead of the first five fields, one of eight special strings may appear:

string meaning ------ ------- @reboot Run once, at startup. @yearly Run once a year, "0 0 1 1 *". @annually (same as @yearly) @monthly Run once a month, "0 0 1 * *". @weekly Run once a week, "0 0 * * 0". @daily Run once a day, "0 0 * * *". @midnight (same as @daily) @hourly Run once an hour, "0 * * * *". 

Please note that startup, as far as @reboot is concerned, is the time when the cron(8) daemon startup. In particular, it may be before some system daemons, or other facilities, were startup. This is due to the boot order sequence of the machine.

This is far from being the only way to run something at boot time, but it’s an alternative.

Источник

How can I configure a service to run at startup

but it’s not configured to auto start when the PC reboots. How can I configure it to start automatically, even if no one is logged into the PC?

@user154721 What arguments did you give update-rc.d to make autostart work? I’ve tried various options but haven’t had any luck.

Auto-start programs are configured in autostart manifests or in *.service files in several locations, as well as in init.d or crontab . See: unix.stackexchange.com/a/525845/43233

5 Answers 5

sudo update-rc.d minidlna defaults 

This should add the service to the automatic startup system. But if you get:

System start/stop links for /etc/init.d/minidlna already exist. 
sudo update-rc.d minidlna enable 

P.S.: For further detail look at the man page for update-rc.d by typing the command man update-rc.d

thanks, but what if I get this output when running that command?: System start/stop links for /etc/init.d/minidlna already exist.

if minidlna doesnt write to any logfile you won’t find it any where. Does it maybe fail because there is no network connection when it is started? Try to look at the configuration if it is possible turn on debugging and/or logging to find out whats wrong.

I am also, got the same error update-rc.d: /etc/init.d/mongod: file does not exist . Then, sudo systemctl enable mongod.service worked for me.

Since Ubuntu 15.10 and newer (resp. Debian 8 «jessie» and newer), you have to use the following command to configure your service minidlna to run at startup:

sudo systemctl enable minidlna.service 

And to disable it again from starting at boot time:

sudo systemctl disable minidlna.service 

This works with all service names available on your system. To find out available service names, just list the filenames of the service files:

ls /lib/systemd/system/*.service ls /etc/systemd/system/*.service 

As for sudo, I recieve Unknown operation enable. If I discard sudo it will ask me which user I would like to choose. Pick your root user and you will be able to set the desired value.

@tanius I have followe the procedure of adding new services to /etc/systemd/system/*.service. I would like to know why no one suggested creating or editing these files as it seems to provide mode options (Restart, RestartSec, ExecStop, etc. )

@tiagoams Creating a service file was just not the question here, since the OP states that a service minidlna already exists but does not autostart. But thanks for the pointer to service files under /etc , I added that to the answer now.

update-rc.d service_name defaults 
update-rc.d -f service_name remove 

defaults => default run levels 2,3,4 and 5

update-rc.d tomcat7 defaults 

When I do this command, I get «System start/stop links for /etc/init.d/tomcat7 already exist». however, when I reboot, it does not start tomcat, I always have to do «service tomcat7 start».

in my case sudo update-rc.d myservice default always completes with no output and the service won’t start on boot-up or with sudo service myservice start which also completes silently. sudo /etc/init.d/myservice start works however

Sometimes you need to run a script on boot process, for example run an iptables config at boot process. So you don’t have to run the script manually every rebooting.

You can run your script on boot process in Ubuntu by adding it to /etc/init.d/rc.local file. Look the steps below.

    Open /etc/rc.local file with this command:

sh /home/ivan/iptables.sh echo 'Iptable Configured!' 

Comments of /etc/init.d/rc.local in Ubuntu 12.04 says «Short-Description: Run /etc/rc.local if it exist», so maybe adding the scripts to /etc/rc.local would be better idea?

Vadim, rc.local is just run at boot, nothing is achieved on shutdown. The process would most likely be killed by the OS on shutdown.

FYI: The difference between rc.local vs adding it to init, is that rc.local is executed at the end of the init startup sequence, rather than as part of it

In ubuntu version 18.04 TLS, I found that update-rc.d does not work fine if there is no specific comment block in the start script that looks like this:

### BEGIN INIT INFO # Provides: myprogram # Required-Start: $ local_fs $ remote_fs $ syslog $ network $ time # Required-Stop: $ local_fs $ remote_fs $ syslog $ network # Default-start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: myprogram some description ### END INIT INFO 

Источник

Читайте также:  Sinclairos macosx ubuntu linux
Оцените статью
Adblock
detector