Applescript waiting for close (I'd like it not to) - concurrency

In Applescript, I have the following:
do shell script "/Applications/Vidalia.app/Contents/MacOS/Vidalia"
do shell script "/Applications/Firefox_3.6/Firefox.app/Contents/MacOS/firefox-bin -P Anon"
It works perfectly, but the issue is that it will wait for Vidalia to be CLOSED before it will then launch Firefox. I would like to open both at the same time with one script. I don't really understand the language and all of my searching has turned up nothing. How can I get these open simultaneously? That is the point, after all.
Any help is much appreciated.

Try using the open command to launch the applications:
do shell script "open /Applications/Vidalia.app"
do shell script "open /Applications/Firefox_3.6/Firefox.app --args -P Anon"

With do shell script, AppleScript will wait for the response of the process you're running. As you're calling it, there's no option but to wait for the process to terminate, which is when the application closes.
To solve this, you need to replace your shell commands with ones that provide no response and terminate right away. So try this:
do shell script "/Applications/Vidalia.app/Contents/MacOS/Vidalia &> /dev/null & /Applications/Firefox_3.6/Firefox.app/Contents/MacOS/firefox-bin -P Anon &> /dev/null &"
I didn't test this with the applications you're using, as I have neither installed; instead, I tested with iTunes and Bento, with which it worked as you're hoping.

Related

Snooping on pseudo terminal

I want to write a program that can capture the input/output of a pseudo terminal without it affecting the original terminal. It can be likened to pointing script to a /dev/pts/<n>.
Use Case: A user ssh's into my machine and runs an interactive tool. With audit, I can see commands running but I need to see the output also. I can listen in on /dev/pts/<n> but then the original logged in user does not get the output.
I want to write my own program to handle this case. Is this problem actually solvable and if so, where should I be looking to find a solution?
That's solvable by using ptrace(2) on the ssh server process which handles to master end of the pseudo-terminal (which is usually the parent process of the shell running in the terminal).
You can start with strace which is itself using ptrace(2), e.g.
strace -p <pid> -e trace=read,write \
-e read=<fds opened to /dev/ptmx> \
-e write=<fds opened to /dev/ptmx>
This will show you everything that's read or written to that pseudo-terminal. You can get the "fds opened to /dev/ptmx" from ls -l /proc/<pid>/fd.
You can then look at what strace is doing -- e.g. by stracing strace itself with
strace -e trace=ptrace,process_vm_readv strace ...
and by studying its source code.
You can of course modify the ssh server itself to log all that info, or just tweak its config options (e.g. LogLevel -- which can be modified on a per-user or connecting host basis).

Calling multiple commands via system() under windows doesn't work

So basiclly im trying to use this:
int main()
{
system("adb kill-server \n"
"adb devices \n"
"adb start-server & \n"
"var=$(adb shell \"pidof com.rok\")\n"
"AFTER=`echo $var | sed 's/\\r//g'`\n"
"echo \"$AFTER\"\n"
"adb shell \"kill -STOP $AFTER\"\n"
"adb shell sleep 2\n"
"adb shell \"kill -CONT $AFTER\"");
return 0;
}
thing is this works in Clion without any error, but i must do this in visual studio and in visual studio i cannot do it like that i have to do every system command alone like:
system("adb kill-server");
system("adb devices");
system("adb start-server");
system("var=$(adb shell \"pidof com.rok\")");
system("AFTER=`echo $var | sed 's/\\r//g'`");
system("adb shell \"kill -STOP $AFTER\"");
so now the thing is when i run it like this everything works except this two lines:
system("var=$(adb shell \"pidof com.rok\")");
system("AFTER=`echo $var | sed 's/\\r//g'`");
even though they perfectly works on clion they dont in visual studio, i cannot find a way to solve this problem, this is the error:
'var' is not recognized as an internal or external command,
operable program or batch file.
'AFTER' is not recognized as an internal or external command,
operable program or batch file.
can anyone explain why this happens? and how can i solve this problem?
Each call of system() creates it's own shell as a subprocess, that's why multiple subsequent system() calls don't work if you e.g. try to set shell variables or do a cd command (expecting subsequent commands running in a specific directory).
The easiest way to do it, is to create a little temporary script file containing all the commands and execute this one with a single system() call:
// A raw string literal to make escaping easier
// (see https://en.cppreference.com/w/cpp/language/string_literal)
std::string cmds = R"xxx(
adb kill-server
adb devices
adb start-server &
var=$(adb shell "pidof com.rok")
AFTER=`echo $var | sed 's/\r//g'`
echo "$AFTER"
adb shell "kill -STOP $AFTER"
adb shell sleep 2
adb shell "kill -CONT $AFTER"
)xxx";
std::ofstream cmdFile("tempCmdFile.cmd");
cmdFile << cmds;
cmdFile.close();
system("tempCmdFile.cmd");
You probably should tidy up the tempCmdFile.cmd up afterwards (i.e. remove it). But I hope you grasp what the code above does.
Also I am not so sure that
AFTER=`echo $var | sed 's/\r//g'`
will work in a windows shell, as you expect it to do. You probably need a different solution for that, or a *nix like shell to run it (e.g. MinGw provides one).

Libssh2: prevent background task from being killed

I am writing a program that logs into another system via SSH using the libssh2 library. Once logged in, I execute a command using:
libssh2_channel_exec(sshchannel, command)
The command executes okay. However, once I close the channel the process running is killed. In my case, the command (executing a binary executable) will run for a long period of time and my program cannot wait for it to terminate. I've tried issuing the following commands all to the same result (the process is still killed upon closing the channel):
/path/myprog
nohup /path/myprog
nohup /path/myprog &
/path/myprog &; disown
Further, I've observed this behavior for both libssh and libssh2. Is there some option or command I am missing?
Thanks in advance.
you can use the unix at command:
echo "cmd" | at now

How to launch terminal in a specific folder?

I've tried both launching /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal as a process and using std::system to launch it from sh script (open -a /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) with different arguments (detsdir as argument, cd destdir etc.). Nothing works. It either opens in ~/, or it says "destdir is a directory` and logs off.
I've also tried launching it as a process with destdir set as working directory (using QProcess::startDetached). No luck either, it opens in ~/.
You can do it like this, with osascript:
osascript -e "tell application \"Terminal\" to do script \"cd ~/Desktop\""
This will create a new terminal window with working directory ~/Desktop.
QProcess has method setWorkingDirectory()

What is the difference between calling a script from the shell and using system()?

I have built a bash script to start up some processes in my system. It simply calls the process and associated config file. Same as I would call from the command line.
#!/bin/bash
# Start specified process in a new session
setsid $1 &>/dev/null &
So to start up someprocess, I would call from the command line:
root#supercomputer:~# start someprocess
This works like a charm. Every process, every time. But when I make a system call from a different running C++ process, someprocess never starts up.
system( "start someprocess" )
This approach for 90% of my processes, except for one. The only difference in the working and not working processes is that the non-working one uses proprietary libraries underneath. I recently added the setsid option to the bash script in hopes that starting a new session would help, but it made no difference. I've also tried popen, and execv. No change.
So my question is what is the difference between calling something with system() and just making that same call from the command line?
All processes are written in C++ on Linux.
.bashrc is only invoked if bash is run as interactive, non-login shell. If it's invoked as non-interactive shell, as when using system() on a script with a bash shebang, it only reads the configuration file pointed to by $BASH_ENV.
That means you have the following options:
add -l to the shebang - causes the shell to read ~/.profile at startup
set $BASH_ENV to the script you want sourced before calling system()
add -i to the shebang - invokes bash as interactive shell and causes it to read ~/.bashrc, but will also effect how bash handles input/output.
I'd recommend the first option.
You can find a detailed explanation of how bash reads it's startup files here. I'm not sure this will solve your problem completely, but it may at leas shed some light on that part of the issue.
Check the environment variables that are used in the system() call. For example, call system to print out some of the variables, and see if they match what you see from the command line.
Likely they are not being sourced correctly.