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.
Related
So, I've written a C executable on Linux for Windows using mingw32-gcc. It is a basic Input-Output program, you type an input and get an answer.
Problem is the cmd shuts down immediately, so the user can't see the output.
Assuming I cannot use Windows to edit the executable there, what should I change in my code/ what flags should I use when compiling it?
inb4:
the user is supposed to click and run it, so running it from cmd won't help.
adding getchar()/scanf() at the end of my code doesn't work, and it feels a bit like cheating.
SOLVED: so all I had to do was to actually add a getchar() after every scanf() and one more at the end of the code for the user input to close the cmd.
Waiting for input at the end is not cheating, but common practice. How else should program know for how long it should stay opened? Closing program by closing console window directly is more cheating than waiting for user input to finish.
You can for example prompt user to hit any key like Press any key to exit... or something similar.
If you want some specific delay, you can use Sleep() from windows.h instead of waiting for input.
See https://stackoverflow.com/a/3379146/3035795
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 &
I'm going to run a regular program on a Linux-arm embedded device.
I tried to use system(cmd) function to run linux shell cmd in my program.
cmd would be a audio playing command "aplay -N sound.wav"
If cmd is as above, there will be no sound come out of my linux device, and the process of the program will in the T state (traced or stopped).
If cmd is set as "aplay -N sound.wav &", things will work just fine.
My question is what caused that, why does the "&" background parameter matter in this case.
Thanks.
If aplay allows for STDIN to act as a controller, running it forground may not provide the control input it expects. The backgrounding may detach STDIN and have aplay revert to default "play once until finished" mode. Do you have a man page for aplay?
I think i got why.
I'm running my qt program in the '&' mode, so I guess in any system(cmd), that cmd must contains a '&'.
I tried to run my qt program without the '&', after that, the cmd without '&' would be working fine.
So I guess the cause is you cannot run fork a foreground child process from a background father process.
The title kind of says it all:
I'm wondering if there's any _NSWasLaunchedFromFinder-type API or hook that an OS/X C program (of the int main(int argc, char* argv[]) variety) could use to determine if it was launched by a user clicking on the executable in the Finder vs. if it was run through a more traditional route (like being typed into the Terminal).
If you are talking about plain command line utility - there is no way to determine whether it was launched from Finder or in terminal since Finder will launch terminal and then execute your program in it.
But there is a solution. I'd rather call it a workaround. You can wrap your executable with bundle, create simple script (lets call it finderLauncher) which will launch actual executable with some additional command line parameter (-launchedFromFinder for example). Don't forget to make it executable. Than in your Info.plist file set finderLauncher as CFBundleExecutable value.
And now in Finder user will see only your bundle and by clicking on it your actual executable will be launched via finderLauncher passing specified command line parameter. Same behaviour will be by using open command in terminal.
And by direct launch from terminal there would be no -launchedFromFinder parameter (off course if user will not pass it directly).
P.s. It would be much easier by specifying command line parameters directly in Info.plist, but I can't find such key in Information Property List Key Reference although there is such key for agents/daemons.
Method 1 ::
You can use NSGetExecutablePath
Here's the dev reference to it :: Mac Developer Library
_NSGetExecutablePath() copies the path of the main executable into the buffer buf. The bufsize parameter should initially be the size of the buffer. This function returns 0 if the path was successfully copied, and * bufsize is left unchanged. It returns -1 if the buffer is not large enough, and *
bufsize is set to the size required. Note that _NSGetExecutablePath() will return "a path" to the exe-
cutable not a "real path" to the executable. That is, the path may be a symbolic link and not the real
file. With deep directories the total bufsize needed could be more than MAXPATHLEN.
Method 2 ::
Use AppleScript
You can use AppleScript to find the current applications open with the following script ::
tell application "Finder"
set appPath to my getFrontAppPath()
set AppleScript's text item delimiters to {":"}
set currentApp to text item -2 of appPath
say currentApp
end tell
on getFrontAppPath()
set frontAppPath to (path to frontmost application) as text
set myPath to (path to me) as text
if frontAppPath is myPath then
try
tell application "Finder" to set bundleID to id of file myPath
tell application "System Events" to set visible of (first process whose bundle identifier is bundleID) to false
-- we need to delay because it takes time for the process to hide
-- I noticed this when running the code as an application from the applescript menu bar item
set inTime to current date
repeat
set frontAppPath to (path to frontmost application) as text
if frontAppPath is not myPath then exit repeat
if (current date) - inTime is greater than 2 then exit repeat
end repeat
end try
end if
return frontAppPath
end getFrontAppPath
That should get you the application last opened, whether it was Terminal or Finder :)
For Finder you get a response :: "Macintosh HD:System:Library:CoreServices:Finder.app:"
For Terminal :: "Macintosh HD:Applications:Utilities:Terminal.app:"
Get the Parent Process ID. Then browse its Process Status to get its PPID, recursively up to Finder.app or init.
Once you have found the terminal ancestor that is child of Finder.app, you can look at its start time and its arguments (see the -o and -O options in man ps: your keywords should include args and start): if the terminal process started near your programm start time and the arguments include your program name, you know that it has been started by Finder.app.
Probably, you can ignore the times and just look for the terminal's arguments.
You can assume the reverse logic and use isatty.
if (isatty(1)) printf("Launched in a terminal\n");
else printf("Launched by clicking something\n");
This just determines if stdout is a tty. If you launch it from a program, icon, menu etc... it will be false. also if you want to tell if an X server is running and it was launched from a terminal emulator you can use getenv("DISPLAY") which is set when X starts (so it will be NULL if run from the console)
// when the user doubleclicks your program it will be started with a -psn_ parameter
if (argc >= 2 && (strncmp(argv[1], "-psn_", 5) == 0)) {
InfoLogWithClient(L"Init", L"Program %d cannot be started with double-click!", getpid());
return EX_USAGE;
}
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 ...')