Running command on task startup - conemu

Im trying to run a command in conemu when I open up a console from a task.
For example, I want to open a bash terminal and run node -v to see my current node version. My Current task commands look like this:
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pm:/mnt node -v
This opens a bash terminal and executes node -v, but then closes the process, giving me the error
Root process was alive less than 10 sec, ExitCode=0
My expected result is that the output of node -v would be visable, and under that would be a working bash console.
v12.6.0
root#COMPUTER:/mnt/c# _
I had it like this in my previous environment so I know it's possible, but I don't remember how I did it.

You can suppress this error by adding the flag -new_console:n.
This is more a feature than an error; if your process runs and ends in less than ten seconds, ConEmu will keep the tab open until you press a key, just so you can see the message it produces.
More info here!
Edit: You're also able to change the amount of time before this message shows in the settings, or set it to never/always show
Edit 2: To force a 'Press Enter or Escape to close the console' message, append -new_console:c

Related

How to automatically stop a program.c and input a command in Linux?

So i have this program that is supposed to be a (super basic) server in Linux that shows me this menu:
What u want to do?
0 -> Exit
1 -> Add product
2 -> Show products list
3 -> Turn on server
And i want it so that when you put the number 3, the server.c stops, like when you press CTRL + Z and send the commnad "bg" so that the server runs in backgroundg (like when you do the command "./server&") so i can open the client.c and interact with the server.
Example of what i want it to do when you input 3 (on here i did it manually on the propmt):
estgv18708#viriato:~/tp6$ ./server
What u want to do?
0 -> Exit
1 -> Add product
2 -> Show products list
3 -> Turn on server
3
^Z
[1]+ Stopped ./server
estgv18708#viriato:~/tp6$ bg
[1]+ ./servidor &
Thanks in advance! :D
Probably an XY-problem here -- why would you want to do this?
The basic issue is that this presupposes your server program is run from a shell and you want to return to the shell to do stuff. But what is the server going to be doing in the meantime? The shell will have (need) control of the input and output so the server pretty much just needs to wait.
You can get the same effect as ^z by calling kill:
kill(getpid(), SIGSTOP);
will have the server send a STOP signal to itself. The shell (which is monitoring progress of the program), will notice this and print the "Stopped" message and then prompt for what to do. The server will be stopped, and you can resume it with the shell's fg command (or put it into the background with bg). However, if your server program is not run from a shell (it is just running directly in a terminal), this will simply stop the server and nothing else will happen.
More commonly what you want to do here is run a subshell:
system(getenv("SHELL"));
which will start a new shell and will wait for it to complete. When the shell exits (use the exit command or type ^d at the shell prompt) the server will resume.
If you want the server to continue to run while the shell runs, you can use fork+exec to run the shell in the child and continue running the server in the parent, or you can use fork+_exit to run the server as a grandchild and have the parent shell continue, but you need to be careful about using the terminal input and output (the server and shell will be fighting for it).
Cant u just do system("bg"); ?
U dond need to stop the program just need to put it on the background

what's mean “&” in the parameter of command line? [duplicate]

I am a system administrator and I have been asked to run a linux script to clean the system.
The command is this:
perl script.pl > output.log &
so this command is ending with a & sign, is there any special significance of it?
I have basic knowledge of shell but I have never seen this before.
The & makes the command run in the background.
From man bash:
If a command is terminated by the control operator &, the shell
executes the command in the background in a subshell. The shell does
not wait for the command to finish, and
the return status is 0.
When not told otherwise commands take over the foreground. You only have one "foreground" process running in a single shell session. The & symbol instructs commands to run in a background process and immediately returns to the command line for additional commands.
sh my_script.sh &
A background process will not stay alive after the shell session is closed. SIGHUP terminates all running processes. By default anyway. If your command is long-running or runs indefinitely (ie: microservice) you need to pr-pend it with nohup so it remains running after you disconnect from the session:
nohup sh my_script.sh &
EDIT: There does appear to be a gray area regarding the closing of background processes when & is used. Just be aware that the shell may close your process depending on your OS and local configurations (particularly on CENTOS/RHEL):
https://serverfault.com/a/117157.
In addition, you can use the "&" sign to run many processes through one (1) ssh connections in order to to keep minimum number of terminals. For example, I have one process that listens for messages in order to extract files, the second process listens for messages in order to upload files: Using the "&" I can run both services in one terminal, through single ssh connection to my server.
These processes running through the "&" will also "stay alive" after ssh session is closed. Pretty neat and useful if the ssh connection to the server is interrupted and no terminal multiplexer (screen, tmux, byobu) was used.
I don’t know for sure but I’m reading a book right now and what I am getting is that a program need to handle its signal ( as when I press CTRL-C). Now a program can use SIG_IGN to ignore all signals or SIG_DFL to restore the default action.
Now if you do $ command & then this process running as background process simply ignores all signals that will occur. For foreground processes these signals are not ignored.
If you have a command which executes and doesn't return status 0(control of prompt) quickly.
For example:
command gedit launches the default editor gedit UI.
commandeclipse launches eclipse IDE.
Such commands keep throwing the logs of activities in the terminal and don't return the command prompt.
Question is, how to run such commands in background so that, we will get back command terminal and we can use terminal for other tasks.
Answer is: by appending & after such command.
user#mymachine:~$ <command> &
Examples:
user#mymachine:~$ edit &
user#mymachine:~$ eclipse &

when I compile with Scite, command prompt won't show up, why?

when I compile with Scite, command prompt won't show up, why?
I am programming a c program, and it wouldn't pop up once I complied
is it because its not connected? or do I have to connect it, if so how?
I'm using an old version (10 year) so the description may not match yours: on my 'Parameter Dialog' there is a setting called 'Sub-system'.
When sub-system is set to 'Command-line-interface', all child processes are created with pipe to trap child output to the 'Output pane'. No 'Cmd window' in this case.
When sub-system is set to anything else : 'GUI', 'Shell' ... the child process is running free, not communicating with SciTE: either Normal window or Cmd window show- up in this case.
Check your 'Parameter Dialog' or 'cpp.config' file to make sure the 'Go command' is not called under 'Command-line-interface' sub-system.

Opening a new terminal window & executing a command

I've been trying to open a new terminal window from my application and execute a command on this second window as specified by the user. I've built some debugging software and I would like to execute the user's program on a separate window so my debugging output doesn't get intermixed with the programs output.
I am using fork() and exec(). The command I am executing is gnome-terminal -e 'the program to be executed'.
I have 2 questions:
Calling gnome-terminal means the user has to be running a gnome graphical environment. Is there a more cross-platform command to use (I am only interested in Linux machines though)?
After the command finishes executing the second terminal also finishes executing and closes. Is there any way to pause it, or just let it continue normal operation by waiting for input?
You probably want something like xterm -hold.
1) gnome-terminal should work reasonably also without the whole gnome environonment, anyway the old plain "xterm" is enough.
2) you can execute a short bash script that launch your program and at the end reads a line:
bash -c 'my program ... ; read a'
(or also 'xterm -e ...')

output to a new terminal

Suppose I create a thread from my "main" thread, which aims to monitor and print some variables in "main". But "main" is also generating some output, so I want to print the outputs of these two threads separately. How can I redirect the output of the new thread to a new console other than the one "main" thread is using in my c program?
I would output the monitoring information to a file, then use tail -f filename to display it in my other terminal.
That has the advantage that you can run grep or what-have-you on the output as well.
I would go with writing the data to a log file and then using tail -f log_file.txt from another window to watch it since that way you get to keep a copy of the data, but if you decide that you need to use separate terminals then you can just open the new terminal (tty) like a regular file. The main problem with this is that you probably really want it to open a new terminal window for you as well.
Opening a new terminal window to do something like this is tricky and is different between different terminal emulators. Here is another question about opening new terminals from a make file. You could try doing the same thing from your program or from a script that runs your program and run cat or tail in the new terminal to be your log window.
#!/bin/sh
truncate --size=0 ./logfile.txt
xterm "tail -f ./logfile.txt" 2>&1 > /dev/null &
your_program --log-file=./logfile.txt
I don't currently know of a better way to accomplish this.
Another thing you might want to look into is syslog.
What would you expect to happen in such case (externally)?
If you run a program you are attached to a console. You of course don't have to write output to the console (you can use files), but the console cannot be duplicated obviously.
Isn't using a different file descriptor good enough for you?

Resources