简体   繁体   English

如何从引导到连续关闭运行 Shell 脚本

[英]How to Run a Shell script from Boot up to shutdown continuously

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:-我正在使用 Linux Ubuntu 18.04 问题是:-

I need to run a python file, which itself in a loop to check the time and greet at every half a hour.我需要运行一个 python 文件,它本身在一个循环中检查时间并每隔半小时打招呼。 I included that file and its path in the .sh file .我将该文件及其路径包含在.sh 文件中。 Now I need to run this sh file from Boot up to shutdown continuously and greet the user.现在我需要从 Boot up 运行这个sh文件以连续关闭并问候用户。

Another question: I tried with crontab but its not working in the startup time.另一个问题:我尝试使用 crontab,但它在启动时不起作用。 I assigned it for 30 mins gap.我分配了 30 分钟的间隔。 so each 1:00, 1:30, 2:00, 2:30 its working, If i switched ON at 1:15 its not showing any output.所以每个 1:00、1:30、2:00、2:30 都在工作,如果我在 1:15 打开它没有显示任何 output。 It waits till 1:30 to reach.它等到1:30到达。

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谢谢回复

I'd go for systemd-timers: https://wiki.archlinux.org/title/Systemd/Timers我会为 systemd-timers使用 go: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.它很简单,它是可移植的,它提供了许多不同的方式来配置启动时间和重复时间。

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:您需要记住,为了在指定的时间或间隔运行 systemd 服务,您将需要两个 systemd 单元:

  1. The systemd service unit that will execute your script.将执行您的脚本的systemd 服务单元。 You can check out this answer on how to set up a systemd service (for your particular use case, skip the last step ie do not start your service unit).您可以查看有关如何设置 systemd 服务的答案(对于您的特定用例,请跳过最后一步,即不要启动您的服务单元)。

  2. 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 .最好使用相同的名称命名您的服务单元和计时器单元,例如,如果您有my_service.service ,您将在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.您需要确保正确配置了OnCalendar变量。 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.此外,@Andreas 提出的文档可能是了解如何设置此变量的一个很好的切入点,更重要的是,了解 systemd 服务和计时器的工作方式。

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).最后,您只需要enablestart定时器单元(定时器单元)。 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 系统定时器

You can use systemd or a Service, but you can also use Crontab:您可以使用 systemd 或服务,但也可以使用 Crontab:

@reboot /your/script.sh
*/30 * * * * /your/script.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM