7

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.

5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210
  • I don't have the full details now, but you need to write a systemd service that runs itself after normal startup. Also, more details about when exactly the script needs to run would be helpful. – Ignacio Vazquez-Abrams Feb 09 '15 at 22:24
  • StackOverflow is for programming questions. Please use the `flag` link at the bottom of your question and ask the moderator to move it to http://unix.stackexchange.com. Good luck. – shellter Feb 09 '15 at 22:55
  • I'm voting to close this question as off-topic because it isn't programming-related. – womble May 24 '15 at 11:49

3 Answers3

20

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

Charlie C
  • 311
  • 1
  • 5
2

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

Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51
  • I read somewhere saying it is not available/good for Fedora 20 and later. – 5YrsLaterDBA Feb 09 '15 at 22:35
  • you will have to enable it: `systemctl enable rc-local.service` – Tiago Lopo Feb 09 '15 at 22:36
  • I got the following after I inserted my line to invoke my script: $ sudo systemctl enable rc-local.service The unit files have no [Install] section. They are not meant to be enabled using systemctl. Possible reasons for having this kind of units are: 1) A unit may be statically enabled by being symlinked from another unit's .wants/ or .requires/ directory. 2) A unit's purpose may be to act as a helper for some other unit which has a requirement dependency on it. 3) A unit may be started when needed via activation (socket, path, timer, D-Bus, udev, scripted systemctl call, ...). – 5YrsLaterDBA Feb 09 '15 at 22:43
  • Yes, I read that answer and tried it. I checked it to executable and added that bash line and also reboot my machine. The same as above. – 5YrsLaterDBA Feb 09 '15 at 22:59
  • same for me also, any solution you found? –  Aug 10 '17 at 20:40
2

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

John_West
  • 2,239
  • 4
  • 24
  • 44
IAmAliYousefi
  • 1,132
  • 3
  • 21
  • 33