Run GO program in background (in Terminal)?

9,374 views
Skip to first unread message

nvcnvn

unread,
May 18, 2011, 6:34:25 AM5/18/11
to golang-nuts
#I know that this may not a GO question but the fact that I meet Linux
as the same time with GO! :D

At the GO TUT, we run a program by type: ./program-name and press Ctrl
+ C for stop it.

But I like my program run like...hmm how can I say? I want to turn it
on and close the terminal without turn it of!

How can I do it!? :D

John Beisley

unread,
May 18, 2011, 6:54:39 AM5/18/11
to nvcnvn, golang-nuts
The common and easy way is to do:

nohup ./program-name &

The & at the end runs the program in the background, so that it
doesn't block your terminal. The "nohup" bit makes the program "safe"
when your terminal hangs-up - i.e it carries on running - by default
the output of the program gets put into a file "nohup.out".

If a program should do this by itself, you might need to daemonize it,
although I've not looked into how this might be done in Go.

- Huin

Nick

unread,
May 18, 2011, 7:47:19 AM5/18/11
to golang-nuts
There is an issue filed for Fork() and Daemonize(), it doesn't appear
high priority at the moment.

http://code.google.com/p/go/issues/detail?id=227

André Moraes

unread,
May 18, 2011, 8:22:03 AM5/18/11
to golang-nuts
Besides the

nohup ./program-name &

You could use

nice 19 ./program-name &

The nice change the priority of the program, so running "nice 19 <you
command here>" will put your process with a very low priority.
This is good because if your program starts an infinite loop, it will
no hang the entire system.

--
André Moraes
http://andredevchannel.blogspot.com/

nvcnvn

unread,
May 18, 2011, 8:23:21 AM5/18/11
to golang-nuts
THANKS ALL OF YOU!!!!
I can do it now by using the "&" at the end but now I got the new
issue......how can I stop it!? :D

Wei Cheng

unread,
May 18, 2011, 8:28:12 AM5/18/11
to golang-nuts
killall program-name

if failed

sudo killall program-name -signal KILL

Canopée

unread,
May 18, 2011, 8:28:34 AM5/18/11
to golan...@googlegroups.com
Recent linux user here.


You'll have to learn a few commands, like ps or kill or chmod.
You may be interested by this script I use on my server to kill, clean
and launch my program (whose name is gogo) :

rm -f _go_.6
rm -f _go_.8
killall -q gogo
gomake
mv gogo.out gogo.out-old
mv gogo.err gogo.err-old
nohup ./gogo > gogo.out 2> gogo.err < /dev/null &

(I put all this in a .sh file)

When I want to see the output of my program (a web server) I simply
type this :

tail -f gogo.out

André Moraes

unread,
May 18, 2011, 8:39:34 AM5/18/11
to golang-nuts
Let's say that I have a program "goproxy" --I do have it, its a web
frontend server that I am working on :)

nice 19 ./goproxy &

Now I want to stop it

ps aux | grep goproxy

This will print all the commands that have the "goproxy" string in the name.

The first column that have a number on it represents the process ID
(PID) of the process.

To kill (stop the process) you just type

kill <put here the PID>

And remove the <> from the comand.

Also, you can search more information about those commands by typing

man kill
man grep
man ps

You can use the "top" command too. This command shows a list of all
running process with lots of information.
It's a little confuse at first, but later you get used to it.

Again, you can use

man top

To get more infor on the command.

Hope it helps

The following links are a good source of information about linux for beginners

http://www.linuxquestions.org/questions/linux-newbie-8/
http://kernelnewbies.org/IRC

nvcnvn

unread,
May 18, 2011, 8:59:37 AM5/18/11
to golang-nuts
:X Thanks all of you one more time!

Rich

unread,
May 18, 2011, 5:19:03 PM5/18/11
to golan...@googlegroups.com
All of these answers are good ones, but I wanted to add a new item in this section. A lot of people forget about the 'at' command. It's a scheduler similar to cron, but its for executing commands at a particular time. So if you want to launch a program in the background, you can use the command 'at now'

[rmasci@easyone ~]$ at now
at> /usr/bin/program1
at> <EOT>
job 336 at Wed May 18 17:04:00 2011
[rmasci@easyone ~]$

You can list more than one item just hit enter at the end of each line
at> /usr/bin/program1
at> /usr/bin/program2
at> /usr/bin/and_so_on

When you're done entering commands, hit ctrl-d and that executes.   You can also pipe entries into it like this: echo "/usr/bin/program1" | at now

I've used this when I wrote a web front end to a password change utility.  The user authenticated with his corporate exchange password, and then could reset his password on 40 Solaris systems that were not connected in to LDAP.  Because of the amount of time it takes to change the password on 40 different servers, I didn't want the user to hang around waiting for his browser to return.  I issued an exec in php to 'echo "passwd_reset someuser somepasswd" | at now"  The passwd_reset program at the end would then send the password reset results in an email.

Another use for the 'at' command is for a report I wrote.  My manager wanted me to generate a report, and wanted that report delivered at 8am, but you had to generate the report at 12am  The 'at' command lets you specify a time so I finished the script with  
echo "mutt -s \"today's report\" $userlist < /tmp/todays_report" | at 0800
Which then scheduled my report to be sent at 0800.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages