简体   繁体   English

Fedora 20如何在启动结束时运行脚本

[英]Fedora 20 how to run script at the end of startup

I am using Fedora 20 . 我正在使用Fedora 20 I have a two lines bash script needs to be run at the end of the startup. 我需要在启动结束时运行两行bash脚本。 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. 我试过“sudo crontab -e”来插入我的可执行脚本,但它总是给我错误,告诉我时间不对,无法修改文件。

You can create a Systemd unit file in /usr/lib/systemd/system/<service_name>.service . 您可以在/usr/lib/systemd/system/<service_name>.service中创建Systemd单元文件。 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. 'WantedBy = multi-user.target'是告诉Systemd在每次启动时运行脚本的神奇之处。

On the command line, tell Systemd to enable your service: 在命令行上,告诉Systemd启用您的服务:

systemctl enable <service_name>.service

The next time you reboot your script should be run. 下次重新启动脚本时应该运行。 Logs will be written to /var/log/messages. 日志将写入/ var / log / messages。

Fedora has some basic documentation on unit files: Systemd unit files Fedora有一些关于单元文件的基本文档: Systemd单元文件

You can append /etc/rc.local it runs just after the system starts up. 您可以在系统启动后附加/etc/rc.local附加它。

You may have to create it if doesn't exist: 如果不存在,您可能必须创建它:

Check this answer 检查这个答案

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: 如果您想使用/etc/rc.local方式,请不要忘记,在编辑后授予此文件的执行权限:

chmod +x /etc/rc.local

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

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