0
sudo nano /etc/rc.local

I have run the above and added the path below. HDD_temp is an executable text file.

/home/matthew/@/HDD_temp/HDD_temp

However, the file is not run at system startups. How can I run it at system startups?

  • My answer is the same as at https://unix.stackexchange.com/a/471871/5132 and https://unix.stackexchange.com/a/333003/5132 and https://unix.stackexchange.com/a/247543/5132 and others before. – JdeBP Mar 01 '19 at 19:01

2 Answers2

3

You can use cron and and the script to be run at @reboot.

To edit: sudo crontab -e.

Then add the task:

@reboot the_script_to_be_run
  • When I ran "sudo crontab -e", I was given three options. I selected " 1. /bin/nano <---- easiest". Did I select the right one? – Matthew Wai Mar 01 '19 at 19:16
  • @MatthewWai If you like the easiest, then I guess so. –  Mar 01 '19 at 19:17
  • I need to run a service file. Can I add this: "@reboot /lib/systemd/system/fancontrol-via-hddtemp.service"? – Matthew Wai Mar 02 '19 at 07:09
  • 1
    @MatthewWai No, a Systemd service is not supposed to be run this way. –  Mar 02 '19 at 07:39
  • The command "sudo service fancontrol restart" needs to be run 30 seconds after the script file "HDD_temp" (not a Systemd service) is run at system startups. What task should be added into Crontab? – Matthew Wai Mar 02 '19 at 11:55
  • @MatthewWai Restarting a service is no big deal I guess, especially this one and after 30 seconds. I'd just add this command to the script. Eg. sleep 30 && sudo service fancontrol restart. Notice that you need to configure sudo, so that it doesn't require authentication. –  Mar 02 '19 at 12:18
  • Please click here to see the script. Under which line should I add this command: "sleep 30 && sudo service fancontrol restart"? I do not need a password to run "sudo". – Matthew Wai Mar 02 '19 at 12:27
  • The script will be run repeatedly so as to monitor the temperature continuously, but I don't need to restart Fancontrol repeatedly. – Matthew Wai Mar 02 '19 at 12:50
  • @MatthewWai You need to decide when exactly and how often you want to restart the service. If it's just once 30 secs after the script is started, then put sleep 30 && sudo service fancontrol restart& at the beginning of the script. –  Mar 02 '19 at 13:34
  • @MatthewWai Not at the very beginning. But before the loop. –  Mar 02 '19 at 13:35
  • @MatthewWai Simply. But then configure sudo not to ask for your password. Or put the script into root's crontab. –  Mar 05 '19 at 05:14
  • I don't need password to run "sudo". How do I put the script into root's Crontab? I added "@reboot /Fancontrol/HDD_temp", which has not run at system startups. – Matthew Wai Mar 05 '19 at 05:16
  • @MatthewWai I don't know. I think cron reports sending mails, so check your mail. But in general I'd check if the script is where you claim it should be and if it has execution bit set. Can you run this command in the terminal? –  Mar 05 '19 at 05:18
  • @MatthewWai To add as root just login as root and crontab -e, or simply sudo crontab -e. –  Mar 05 '19 at 05:18
  • I already used "sudo crontab -e" to add the task. "/Fancontrol/HDD_temp" is the exact location. In the "Elevated Privileges" window, when I double-click on the script file and select "Run", it will be run. When you wrote "check your mail", did you mean email or something else? – Matthew Wai Mar 05 '19 at 05:29
  • @MatthewWai I don't know how clicking and windows work. As for mail, do sudo mail. –  Mar 05 '19 at 05:36
  • @MatthewWai And try /Fancontrol/HDD_temp from the terminal. Just this. –  Mar 05 '19 at 05:37
  • I just ran "sudo /Fancontrol/HDD_temp" via Terminal, and the script is now running properly. Please click here to see the simplest version of the script. It will just create a text file (showing the temperature of /dev/sda) in this folder "/Fancontrol/". Could you put the script in the same folder and add the task into your Crontab? – Matthew Wai Mar 05 '19 at 06:12
2

You can also make a systemd "service". For example:

Create an empty text file using vi, nano or whatever:

[Unit]
Description=My service
[Service]
ExecStart=/path/to/my/script.sh
ExecStop=/usr/bin/killall script.sh

# Useful during debugging; remove it once the service is working
StandardOutput=console

[Install]
WantedBy=multi-user.target

Save it under /etc/systemd/system/myscript.service or any other name,

Then run:

sudo systemctl start myscript

You can check the service with sudo systemctl status myscript and stop with sudo systemctl stop myscript. To make it starts after boot run systemctl enable myscript as root.

  • Thanks a lot! I successfully ran "sudo systemctl enable testing.service", which I don't need anymore. How can I disable the service and delete the "symlink" previously created? I have already deleted the "testing.service" file. – Matthew Wai Mar 06 '19 at 00:10
  • sudo systemctl disable testing.service – Mimi the Cat Dec 25 '19 at 18:08