简体   繁体   English

如何在VM Virtualbox Ubuntu 16.04中启动时运行Shell脚本

[英]How to Run Shell Script at start in VM Virtualbox Ubuntu 16.04

I am trying to run a shell script which runs at system login. 我正在尝试运行在系统登录时运行的Shell脚本。 To try, I used an example script, which has two lines #!/bin/bash echo “Hello World” 为了尝试,我使用了一个示例脚本,该脚本有两行#!/bin/bash echo “Hello World”

I Followed all the instructions mentioned on this website http://www.cyberciti.biz/tips/linux-how-to-run-a-command-when-boots-up.html 我遵循了该网站上提到的所有说明http://www.cyberciti.biz/tips/linux-how-to-run-a-command-when-boots-up.html

Even I did some steps such as editing /etc/rc.local , but still when I login, I do not see Hello world output running on terminal. 即使我做了一些步骤,例如编辑/etc/rc.local ,但仍然在登录时看不到终端Hello world运行的Hello world输出。

Can anyone please explain what is wrong here or may be I am missing something? 谁能解释一下这里出了什么问题,或者我错过了什么?

Looks to me like Ubuntu 16.04 is a systemd system. 在我看来,Ubuntu 16.04是一个systemd系统。

Which means you should create a systemd service to run whatever you'd like @ startup. 这意味着您应该创建一个systemd服务来运行@start所需的任何东西。

Look here https://wiki.ubuntu.com/SystemdForUpstartUsers#Example_Systemd_service 在这里查看https://wiki.ubuntu.com/SystemdForUpstartUsers#Example_Systemd_service
After you made your service, use systemctl to enable it on boot for systemd. 进行服务后,使用systemctl在启动systemd时启用它。
sudo systemctl enable mycustom.service
To start the service. 启动服务。
sudo systemctl start mycustom.service

You can also schedule a cron job to run once after the reboot command. 您也可以计划cron作业在重新启动命令后运行一次。 The line will need "@reboot root /path/script.sh" You can specify whatever user you want to run the script. 该行将需要“ @reboot root /path/script.sh”。您可以指定要运行脚本的任何用户。 To make the result easier to find, you might make that second line something like echo "Hello World" > /root/hello.txt 为了使结果更易于查找,您可以使第二行类似于echo“ Hello World”> /root/hello.txt

Add the line in /etc/profile under echo command as follows 在echo命令下的/ etc / profile中添加该行,如下所示

echo “Hello World”

Then execute the following command 然后执行以下命令

source /etc/profile

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

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