Bash script to update GO project on Ubuntu 16.04

huangapple go评论72阅读模式
英文:

Bash script to update GO project on Ubuntu 16.04

问题

大家好。
我对bash脚本和在Ubuntu上部署Go程序都不太熟悉。
我是这样运行我的Go程序的:

go build -o myprogram main.go
./myprogram &

但是现在,我不想再使用sftp上传文件并手动更改一切,而是想编写一个简单的bash脚本。

问题是,我首先需要终止现有的进程,但我不知道如何获取PID并终止它。

也许我可以使用其他方法来运行我的程序,这样就不需要找到PID了。

我尝试使用ps ax | grep myprogram然后终止它,但没有成功。

英文:

guys.
I am new to bash scripting and deploying Go on ubuntu.
I run my Go program like this

go build -o myprogram main.go
./myprogram &

But now, instead of uploading files with sftp and manually change everything I wanted to write simple bash script.

The problem is that I firstly need to kill existing process and I don't know how to get PID and kill it.

Maybe I can run my program using something different so I don't have to find PID.

I tried using ps ax | grep myprogram and then kill it, but no luck

答案1

得分: 1

bash有一个特殊的变量$!,你可以用它来存储最近启动的后台进程的进程ID。

./myprogram &
myprogram_PID=$!

kill "$myprogram_PID"

这段代码中,./myprogram &表示在后台启动myprogram程序,$!会保存该进程的进程ID。然后,可以使用kill "$myprogram_PID"来杀死该进程。

英文:

bash has a special variable $! that you can use to store the process id of the most recently started background process

./myprogram &
myprogram_PID=$!

kill "$myprogram_PID"

huangapple
  • 本文由 发表于 2017年2月21日 19:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/42366677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定