I have searched so many, but I couldn't find the correct one. Sorry for the repeated question ( Maybe). I'm using Linux Ubuntu 18.04 The question is:-
I need to run a python file, which itself in a loop to check the time and greet at every half a hour. I included that file and its path in the .sh file . Now I need to run this sh file from Boot up to shutdown continuously and greet the user.
Which is the correct method to be used. systemd , init.d, etc/rc.local I also used in .config/autostart/file.desktop but I already have another script. If its wrong please suggest the method to run the script automaticaly from boot up to shut down.
Another question : I tried with crontab but its not working in the startup time. I assigned it for 30 mins gap. so each 1:00, 1:30 , 2:00, 2:30 its working, If i switched ON at 1:15 its not showing any output. It waits till 1:30 to reach.
So please give me a nice method for my 1st question, If 2nd Question have the solution please recommend it too...
Thanks for the reply
CodePudding user response:
I'd go for systemd-timers: https://wiki.archlinux.org/title/Systemd/Timers
It's simple, it's portable and it provides many different ways to configure the startup time and repeat time.
CodePudding user response:
You need to keep in mind that in order to run a systemd service at specified times or intervals, you will need two systemd units:
The systemd service unit that will execute your script. You can check out this answer on how to set up a systemd service (for your particular use case, skip the last step i.e. do not start your service unit).
A timer unit that will define when your service unit will run.
It's a best practice to name your service unit and your timer unit with the same name, so for example if you have my_service.service
, you will configure the timer unit in my_service.timer
.
The timer unit file should look something like this:
[Unit]
Description=Run script every 30 minutes
[Timer]
Unit=my_service.service
OnCalendar=*:0/30
[Install]
WantedBy=timers.target
You need to make sure that the OnCalendar
variable is configured correctly. There are some online generators out there to get the right expression for your desired interval.
Also, the documentation proposed by @Andreas might be a good entry point to understand how to set this variable, and more important, how systemd services and timers work.
Keep in mind, that after making all of those changes, you will need to reload the daemon:
systemctl daemon-reload
Finally, you just need to enable
and start
the timer unit (only the timer unit). So, following the previous notation:
systemctl enable my_service.timer
systemctl start my_service.timer
Documentation
You can read more about these topics here:
Systemd services
Systemd timers