Wednesday, November 5, 2008

Ubuntu Linux Command Line Background Magic (without screen)

This is useful when you are in an ssh session and you want to background a long-running task and log out. The job will continue running after you log out, and you can even log back in later and resume it. This solution does not require screen.

~$ nice -n10 -- super_long_running_script
...

Now say that the script is taking for ever, and you need to kill your ssh session. First, hit + Z

...
[1]+ Stopped(SIGTSTP) nice -n10 -- super_long_running_script
~$

This will stop the task (but not kill it) and give you a console again. Note that the [1] indicates that the task is indexed as job 1. Next, we want to resume the task again, but in the background:

~$ bg 1
[1] nice -n10 -- mysql -u root bamboo_staging <backup_20081105.sql &
~$

the number "1" corrolates with the job number above. The task is now running again in the background. Note that if you log out now bash will kill the task because bash kills its children on exit. To prevent bash from killing the child, do:

~$ disown -h 31968

Where 31968 is the PID of the backgrounded process. Now you can exit your ssh shell:

~$ exit
...

Note that your ssh session doesn't really close properly. However it is now safe to kill your ssh client. The backgrounded process will happily keep running.

Ubuntu/Debian and nfs mount issue solved

I spent a while trying to figure this one out: nfs client mounts were very slow on one of my servers. The dmesg output kept saying:


portmap: server localhost not responding, timed out
RPC: failed to contact portmap (errno -5).


Also, rpcinfo was not working for me:


~$ sudo rpcinfo -p 127.0.0.1
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused
~$ sudo rpcinfo -p localhost
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused


NFS mounts were working but were extremely slow:


~$ sudo mount hostname:/path

...


I was convinced that the problem lied with my hosts file, firewall, or hosts.allow or hosts.deny. It turns out that the problem was simply that a few packages had to be installed:


~$ sudo apt-get install nfs-common
...
Selecting previously deselected package libevent1.
Selecting previously deselected package libnfsidmap1.
Selecting previously deselected package nfs-common.