I want to do this:
Execute an executable from a .sh file
Keep this executable open in terminal
I tried to write a .sh file with "./program" but "program" closes automatically after flashing a console, but I want that terminal to keep open!
I assume you are in a graphical environment, and want to execute a script by double-clicking the ".sh" file, then keep the terminal window open to see any result.
At the end of your script, add something like this :
echo "Hit the [return] key to exit"
read
This will cause execution to pause until you hit [return].
Note : you cannot keep a program running when it has finished executing. So the program you are calling from your script finishes, but then you give the terminal (which is also a program) something to do (waiting for input) to prevent it from closing.
Related
I have a program that creates a temporary file and uses cmd to open it (with the user's default pdf viewer). I want to delete the file after they're done with it.
How would I know when the user closes the file/closes the program that opened it?
I've tried putting an && <do thing> at the end of the command, but it seems to run immediately.
If it matters, it's actually a node project running child_process.exec()
I trigger a bash script from Windows command prompt.
postCloneSetup.sh
It opens another window and then returns. The window it spawned stays open and logs output text.
I want to capture the output from the spawned window and return that to the Windows command prompt.
I would prefer to use something like
$(postCloneSetup.sh) // Linux for capturing output to current context
for the Windows command prompt.
I'd prefer not to modify postCloneSetup.sh. I know I could have it write out to a file with
exec &> postCloneSetupLog.log
but then I must wait and manually run
type postCloneSetupLog.log
to see the output in the console. This is not possible for integrating into a CI engine, which is my goal.
How can I capture the output from the spawned console in one command?
I tried running a python script, but it immediately closes when it encounters an error. Is there a way to stop the console window from closing after an error without using a batch file and without typing this command:
C:\WINDOWS\system32\cmd.exe /K <command>
By the way, adding try and except still doesn't stop the console window from closing. Even using:
except:
sys.exit(0)
Well the console closes as soon as the python program is finished. So if you sys.exit(0) in your except statement, the program finishes and the console close.
Either you write a .bat file next to your python file, that makes sure to call cmd.exe properly, and you run that one. Or you wait in the except statement for some user input/confirmation and sys.exit() only then.
Last solution if you want to see the console only to see the messages, try to print the messages to a file, either using print( "abc", file=opened_file ) in the python code, or adding > filename at the end of the shell command to redirect the standard output to a file.
Personally, I just run the program directly from cmd.exe's shell and never close the terminal.
I have an issue when using this command
system("konsole --new-tab --workdir<dir here> -e perlprogram.pl &");
It opens perlprogram.pl which has:
system("mpg321 song.mp3");
I want to do this because mpg321 stalls the main perl script. so i thought by opening it in another terminal window it would be ok. But when I do run the first script all it does is open a new tab and do nothing.
Am I using konsole correctly?
Am I using konsole correctly?
Likely, no. But that depends. This question can be decomposed into two issues:
How do I achieve concurrency, so that my program doesn't halt while I execute an external command
How do I use konsole.
1. Concurrency
There are multiple ways to do that. Starting with the fork||exec('new-program'), to system 'new-program &', or even open.
system will invoke the standard shell of your OS, and execute the command you provided. If you provide multiple arguments, no shell escaping is done, and the specified program execed directly. (The exec function has the same interface so far). system returns a number that specifies if the command ran correctly:
system("my-command", "arg1") == 0
or die "failed my-command: $?";
See perlfunc -f system for the full info on what this return value signifies…
The exec never returns if successfull, but morphs your process into executing the new program.
fork splits your process in two, and executes the child and the process as equal copies. They only differ in the return value of fork: The parent gets the PID of the child; the child gets zero. So the following executes a command asynchronously, and lets your main script execute without further delay.
my #command = ("mpg321", "song.mp3");
fork or do {
# here we are in the child
local $SIG{CHLD} = 'IGNORE'; # don't pester us with zombies
# set up environment, especially: silence the child. Skip if program is well-behaved.
open STDIN, "<", "/dev/null" or die "Can't redirect STDIN";
open STDOUT, ">", "/dev/null" or die "Can't redirect STDOUT";
exec {$command[0]} #command;
# won't ever be executed on success
die qq[Couldn't execute "#command"];
};
The above process effectively daemonizes the child (runs without a tty).
2. konsole
The command line interface of that program is awful, and it produces errors half the time when I run it with any parameters.
However, your command (plus a working directory) should actually work. The trailing ampersand isn't neccessary, as the konsole command returns immediately. Something like
# because I `say` hello, I can be certain that this actually does something.
konsole --workdir ~/wherever/ --new-tab -e perl -E 'say "hello"; <>'
works fine for me (opens a new tab, displays "hello", and closes when I hit enter). The final readline there keeps the tab open until I close it. You can keep the tab open until after the execution of the -e command via --hold. This allows you to see any error messages that would vanish otherwise.
I have to work on a project in Fedora Linux and I have to type the following very often:
player map1.cfg &
I figured out that I can create an executable .sh file and it contains this:
#!/bin/bash
player *.cfg &
However, when double click on the runmap.sh file it shows me 'Run in Terminal', 'Display', 'Cancel', and 'Run' and when I click 'Run in Terminal' a terminal window opens and closes immediately. If I just hit 'Run' then the .cfg opens but I need the Terminal window to run additional (Java) files.
How can I fix this problem?
Other information:
I use *.cfg because I want to copy and paste the .sh files into other folders that also contain .cfg files such as map2.cfg, map3.cfg, etc.
It's for a Player/Stage project.
When you run a script from the file manager, the shell that is started isn't interactive. The shell can only read the script file.
To open an interactive shell in addition to the files, you can exec the new shell at the end of the script, and use "Run in Terminal":
#!/bin/bash
player *.cfg &
exec /bin/bash
Well let's look at it this way.
When you run the command in a terminal, the command starts as a child process and is then sent to the background. Once the command finishes it terminates. During the time it is running in the background you can still issue commands because your parent process is the terminal window itself.
When you write a script that issues a command to run in the background it is started, spawns the command as a child to it and then closes because the script has finished.
These are a behavior of the OS and something that really shouldn't change. Essentially what you are therefore asking for is a way for it to run the command quickly for yourself yet still leave a command terminal for you to work with?
1) Why is typing the command such a hassle? Bash and other terminals have a history function for this very reason.
2) Why don't you just call the mini script you wrote from a terminal window whenever you need to call the commands. If you put the script in a folder on your $PATH variable it will be available to you in the terminal at any location.