Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fedora 20 how to run script at the end of startup

I am using Fedora 20. I have a two lines bash script needs to be run at the end of the startup. I want it to be run automatically each time when machine is startup. How can I do this?

I tried "sudo crontab -e" to insert my executable script but it always gave me error teling me the the time is not right and cannot modify the file.

like image 978
5YrsLaterDBA Avatar asked Feb 09 '15 22:02

5YrsLaterDBA


3 Answers

You can create a Systemd unit file in /usr/lib/systemd/system/<service_name>.service. Here is a template:

[Unit]
Description=<description_string>

[Service]
WorkingDirectory=<working_directory>
Type=forking
ExecStart=/bin/bash <absolute_path_to_script>
KillMode=process

[Install]
WantedBy=multi-user.target

Replace anything in the angle brackets with your specific information. The 'WantedBy=multi-user.target' is the magic that tells Systemd to run your script on each start.

On the command line, tell Systemd to enable your service:

systemctl enable <service_name>.service

The next time you reboot your script should be run. Logs will be written to /var/log/messages.

Fedora has some basic documentation on unit files: Systemd unit files

like image 74
Charlie C Avatar answered Sep 20 '22 21:09

Charlie C


You can append /etc/rc.local it runs just after the system starts up.

You may have to create it if doesn't exist:

Check this answer

like image 21
Tiago Lopo Avatar answered Sep 21 '22 21:09

Tiago Lopo


Charlie's answer is better but you can still use Tiago's answer.

Just don't forget if you want to use /etc/rc.local way, grant execution permission to this file after editing:

chmod +x /etc/rc.local

like image 20
Ali Yousefi Sabzevar Avatar answered Sep 23 '22 21:09

Ali Yousefi Sabzevar