linux - 启动时自动运行脚本 : script doesn't work correctly

标签 linux bash ubuntu startup autorun

我想在装有 Ubuntu 的 Parallella 开发板启动时运行一个 bash 脚本。我在 SO 中搜索,并在此处找到了说明:

我的 bash 脚本是 test.sh,只有一行:

echo "Hello World" &> /home/qsp/WIP/test/hello.txt

1) 我尝试的第一种方法是将脚本的绝对路径添加到 /etc/rc.local:

/home/qsp/WIP/test/test.sh

2) 我尝试的第二种方法是遵循上面接受的答案。

sudo mv test.sh /etc/init.d/
sudo update-rc.d test.sh defaults 

在这两种情况下,脚本都是在启动后执行的,并且在文件夹中创建了一个文件hello.txt。但是,文件的内容是空的(所有者是 root)。我想知道我是否遗漏了什么。谢谢。

======更新=======

根据 Skynet 的回答,我将脚本更改为:

echo "Hello World" | tee /home/qsp/WIP/test/hello.txt

脚本在正确启动后写入文件。我还有一个问题,为什么我的原始脚本 &> 不起作用,尽管如果从命令行运行它仍然有效。

最佳答案

您应该按照第一个 SO 问题所引用的初始化脚本样式进行制作。像这样:

case "$1" in
start)
    #startup code
    ;;
stop)
    #stop code
    ;;
restart)
    #restart code
    ;;
esac

另请查看 https://github.com/fhd/init-script-template/blob/master/template

关于linux - 启动时自动运行脚本 : script doesn't work correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540343/

相关文章:

python - 包控制 : The dependency 'markupsafe' is not currently installed

tomcat - 更改端口后在 Apache Solr 中获取 HTTP 404

C- Linux- 创建线程并传入缓冲区

java - 如何诊断我们的 java IP 多播应用程序?

c - 如何等到窗口被映射和可见

linux - 脚本如果 ls -lrtd 返回文件或目录不存在该怎么办

c++ - Linux c/c++ 如何从 ThreadID 中查找 PID

linux - 用下划线替换空格并小写 - 文件名

windows - Linux 相当于 Windows 管道

linux - Bash Shell 脚本 - 剪切