Search code examples
bashshellbackground-foregroundjob-control

Shell script: spawn a terminal running a command, with the ability to suspend/resume job within the newely spawned terminal


I would like to use a shell-script or shell-command to spawn/open a new terminal, and immediately run a command in it. However, it should also give me the ability to suspend and later resume the command/program that the new terminal is running.

When I try to do this, I am able to spawn the new terminal with the program running in it, but whenever I try to suspend it, the terminal hangs.

Essentially, my use case is to run a script that opens a text file for editing in nano, with the ability to suspend nano, as you can normally do if it were launched interactively from a shell and not via a script.

I searched a lot, but I never found a solution.

I tried many things, but always failed. Without shell-script, what I do is to spawn xterm, cd to the folder containing file.txt and then issue nano file.txt in the terminal. Nano opens to edit the file, and I am able to suspend it (^T^Z), do whatever I need to do, and resume it once I am done.

However, if I run it via a script, e.g.

#!/bin/sh
xterm -e "nano file.txt"

nano opens fine, but if I try to suspend it (^T^Z), the terminal hangs.

Why does this happen? Is there any way to accomplish what I want?


Solution

  • You can set job control with set -m:

    xterm -e bash --rcfile <(echo 'set -m; nano file.txt')