hapless. Easily run and manage background processes
In the previous article we’ve talked about running and tracking processes in the background with the tools that Linux provides. Fortunately, there is a much easier and convenient way to do that using hapless package. This is a Python project I’ve been recently working on and you can install it using pip like this
1 | $ pip install --upgrade hapless |
The only thing you need to add is a short prefix hap run
to a command you want to run in the background
1 | $ hap run ./my_long_running_script.sh |
It will automatically handle output redirection and detach process from current shell, so it is safe to launch it within remote ssh session, close the connection and return back in a while. To retrieve status of the processes and get list of all the tracked ones simply type
1 | $ hap |
On a creation it will also generate unique id for each process started, but you can also provide your own alias using -n
/--name
flag to refer the process later
Besides that you can specify process by providing its sequential number, so all the invocations below are valid
1 | $ hap status 1 # just a number from the first column |
There is also a -v
/--verbose
flag for the status
/show
command, so you can get detailed information about the process including complete environment and paths to output files.
Options available
Next thing you usually want to do is to check an output for the process (both stdout and stderr is possible). Keep in mind that you can refer to process the same way as above
1 | $ hap logs hap-dmp4ch # show stdout |
Another useful feature is the ability to pause execution of the process and resume it at any point
1 | $ hap pause hap-23i0lo |
Moreover, you can send any arbitrary signal to the process through hapless
1 | $ hap signal hap-23i0lo 9 # sends SIGKILL |
Cleanup
hapless stores information about processes internally and preserves data upon completion, so if you don’t care about finished processes you might want to invoke hap clean
which will leave only ongoing ones (note that in the summary table the PID of the finished processes is dimmed as those do not exist anymore).
That’s almost it as the main idea is to make launch command as easy as possible and have a convenient way to quickly check status of processes started (simply hap
). For the rest of options available invoke hap --help
or check the link below for the documentation.