Mac OSX Bash Script stop processing while specific window is open - bash

I actually wrote a script that is compiling my LaTeX files and open the generated PDF in a viewer. That works fine.
cd Documents/my-bachelor-thesis/
latexmk 000_Root_Bachelor_Thesis.tex -pdf
open 000_Root_Bachelor_Thesis.pdf
ps -A | grep -m1 vorschau | awk '{print $1}'
So with the last line I get the PID of the process my PDF is opened.
There is the problem: I want stop my script process at the point the PDF is open. After I click on the close sign of the viewer, the process should continuo automatically. Is that somehow possible.
Current solution: I interrupt the process while waiting for some user input. After i type in something the process goes on.
echo "Can I proceed?"
read input
... more script
Thanks for helping me out.

Not familar with open in OSX. Quick search on man page suggests that you can force open to start new application, and wait until it finish by
open -Wn 000_Root_Bachelor_Thesis.pdf
Options:
-W Wait until the applications exit (even if they were already open). Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment variable.
-n Open a new instance of the application(s) even if one is already running.

Related

Export/Stream Mac Terminal Output To File

I am running a php script on my Mac terminal which takes hours and hours to run. It consumes memory very quickly, and after a while the scroll back gets truncated. Settings on the Terminal: Scrollback - Limit to available memory.
Is there a way to automatically stream (or just save) the output into a file (whether on local disk or on external harddrive). Also I realise the memory doesn't get cleared until I restart my com (my Finder indicates 0 space on my harddrive after a while but when I restart my com it becomes 20GB). Is there a way to clear this once my output is saved?
It will be nice to include the timestamp as well in the file.
Run your PHP script in the background (or even with nohup as well, if you want to be able to log out and leave it running), and save your output to a log file on disk like this:
someScript.php > log.txt &
Now, if you want to watch the log file growing at a later point, just use the -f option to tail to follow the log:
tail -f log.txt
If you see that all is well and the job is still running, press CTRL+C and you will stop following it but the job will continue. If you want another look later, just run tail again.
If you want to see if your script has passed, say "PHASE 2", just grep for that in the log file:
grep "PHASE 2" log.txt
If you want to timestamp the lines, I would suggest you use the ts utility which is part of moreutils, so hoping you use homebrew to manage packages (as any sensible Mac user does), you would install it with:
brew install moreutils
Then you could do:
someScript.php | ts > log.txt &

using Sublime Text 3 cli from within another service to open an new window creates a temp file

I've been setting up a Karabiner (Launcher Mode) shortcut to open a new Sublime Text window.
Normally I call subl -n from a terminal window to bring up a new sublime window. Karabiner lets you specify shell scripts as shortcuts to run, so I tried /usr/local/bin/subl -n
It works, but, whereas calling subl -n from a terminal window (or /usr/local/bin/subl -n) opens a new window with a "blank" file, calling it through Karabiner opens a temp file at tmp/subl stdin YEd3Bc.txt (YEd3Bc is six random numbers/digits) in a new window
It also blocks Karabiner from launching anything else until you close the window, as if I had called subl -n -w "tmp/subl stdin YEd3Bc.txt"
I thought it was a problem with Karabiner, and tried to work around it by using Automator and Applescript to create wrappers for a shell script, but both of them had the same problem, opening temp files at /tmp/subl stdin random.txt
I haven't used that particular software (Karabiner) however it could be opening a temp file due to the user that is launching the command (Karabiner) not having access to the default path to store the temp file there to which it creates a unique named file in the /tmp directory. You could try adding the following su -u "your user without quotes" -c /use/local/bin/subl -n. As for the locking of (Karabiner) that is most likely due to the internals of Karabiner and how it opens a sub process to the subl binary along with sublimes nature of staying at the foreground. You can attempt a & at the end of the command to send it to the background allowing Karabiner the ability to continue processing.

How to open xlsx file withough hanging a perl process

I am trying to open a xlsx file in a Perl script with the command :
system("path_to_file\\file.xlsx");
The file is opened but it keeps the perl process hanged until I close the .xlsx file. How can I avoid that?
The file file.xlsx is created in the perl script. At the end of the execution I want it to be displayed to the user (like you would manually open it) and it works but it keeps the perl process hanging until i close it.
On Windows, system(1, $cmd) will run the command and immediately return without waiting for the child to finish. See perlport.
On Unix, fork and exec will work to this end.

Get list of open files (descriptors) in OS X

I would like to get a list of open files in a process on os x (10.9.1). In Linux I was able to get this from /proc/PID/fd. However I'm not sure how to get the same on OS X. I found that the procfs is not present on the OS X (by default. possible implementations present, but I do not want to go that way).
So how do I get (natively) the list of open files in a process on OS X. One way is lsof. is there any other support available? please let me know where I can get more info on this.
Thanks.
I had a hard time getting Activity Monitor to show open files for a process that was running as root (via sudo). The original question mentions lsof, and it does the trick exactly. If you know the process name or PID, it's super quick.
Find processes by name:
lsof -c processname
Find processes by PID:
lsof -p 123
(Prefix with sudo as needed, such as if you are not the owner of the process.)
At least on OSX 10.10 (Yosemite, didn't check on Mavericks), you can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup.
Tip: cmd+f shortcut on that pane allows for searching and highlighting on the content.
This works for some things:
sudo fs_usage | grep dev
for /dev/ files or similar.
The clean and simple approach to inspect the current process (i.e. the equivalent of /proc/self/fd on Linux) is to use ls /dev/fd/:
e.g.
$ touch "file"
$ exec 3<>file
$ ls /dev/fd/
0 1 2 3
List open files on /Volumes/VolumeName:
lsof | grep "/Volumes/VolumeName"
"You can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup."
But "you had a hard time getting Activity Monitor to show open files for a process that was running as root (via sudo) or others user".
So just run Active Monitor by sudo, that`s it
sudo /Applications/Utilities/Activity\ Monitor.app/Contents/MacOS/Activity\ Monitor
I use the What's Open application that is very handfull (with filters, disk selection, ...).
You can find it there : http://whatsopen.en.softonic.com/mac.
Since you asked "Is there any other support [than lsof] available?", try this:
Create a command line tool using the "proc_pidinfo" C API referenced in the selected answer to this question: How can I programmatically get the list of open file descriptors for a given PID on OS X?
You can use proc_pidinfo with the PROC_PIDLISTFDS option to enumerate the files used by a given process. You can then use proc_pidfdinfo on each file in turn with the PROC_PIDFDVNODEPATHINFO option to get its path.
lsof -c /^74016$/ -d^txt -FcfadDtns0
The -F instructs lsof to produce output suitable for consumption by another process, such as Perl or awk. In man lsof search for OUTPUT FOR OTHER PROGRAMS for details. The characters that come after -F represent a field you want to select for output. The output then puts each of these characters in front of the respective field. Example output:
p212^#cloginwindow^#
fcwd^#a ^#tDIR^#D0x1000004^#s704^#n/^#
f0^#ar^#tCHR^#D0xf218cacb^#n/dev/null^#
f1^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f2^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f3^#ar^#tREG^#D0x1000004^#s216424^#n/Library/Application Support/CrashReporter/SubmitDiagInfo.domains^#
f4^#ar^#tREG^#D0x1000004^#s77^#n/private/etc/security/audit_user^#
f5^#ar^#tREG^#D0x1000004^#s652^#n/private/etc/security/audit_class^#
f6^#ar^#tREG^#D0x1000004^#s358^#n/private/etc/security/audit_control^#
f7^#ar^#tREG^#D0x1000004^#s111033^#n/System/Library/Frameworks/CoreImage.framework/Versions/A/Resources/ci_stdlib.metallib^#
f8^#au^#tIPv4^#d0xc401abd77f1dd1d9^#n*:*^#
f9^#ar^#tREG^#D0x1000004^#s308316^#n/System/Library/Frameworks/CoreImage.framework/Versions/A/Resources/ci_filters.metallib^#
f10^#au^#tREG^#D0x1000004^#s1536^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/3902/libraries.maps^#
f11^#au^#tREG^#D0x1000004^#s65536^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/3902/libraries.data^#
f12^#au^#tREG^#D0x1000004^#s1536^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/Intel(R) HD Graphics 630/functions.maps^#
f13^#au^#tREG^#D0x1000004^#s131072^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/Intel(R) HD Graphics 630/functions.data^#
p421^#ccoreauthd^#
fcwd^#a ^#tDIR^#D0x1000004^#s704^#n/^#
f0^#ar^#tCHR^#D0xf218cacb^#n/dev/null^#
f1^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f2^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
p537^#cUserEventAgent^#
fcwd^#a ^#tDIR^#D0x1000004^#s704^#n/^#
f0^#ar^#tCHR^#D0xf218cacb^#n/dev/null^#
f1^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f2^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f3^#au^#tunix^#d0xc401abd77b9c8579^#n->0xc401abd77b9c8709^#
f4^#au^#tunix^#d0xc401abd77b9c7129^#n->0xc401abd77b9c8899^#

How to share the screen output of a running process?

I have a program that will run for a long time , This program edit and run in remote server .I use
the computer in office to remote connect the server and run it . The progress shows on the computer screen of the computer in my office ...
I want to see the output in my home , How can I capture the output which is on the screen of computer in office and see the result at home?
I think about writing the output to a file , but I need to close the file . So I should open file then write output , close .. open again?
thanks
I don't know the proper tag should use , but the program is written in perl .....
you can tee it
your_program.pl | tee logfile.txt
and see the lastest result in logfile.txt at home with
tail -f logfile.txt
Why not just redirect to a log file and tail it (or load it in an editor etc.) ?
$ myprog.pl >/tmp/logfile 2>&1
The above redirects your output to a log file (/tmp/logfile - you may wish to choose a better location since /tmp is temporary and can be trashed during a reboot) and redirects stdout/stderr to the same place. Note that this captures the output of your program and you don't need to modify your script.
An alternative is to run your program within screen
Perhaps one of the most useful features is that you can re-connect to
your sessions after you log out, move to another computer, or get
disconnected, and continue working as if nothing happened. All your
edit sessions, shell windows, etc. will be there just as you left
them.
Personally, I use screen for this sort of thing.
Connect to the server
Enter the command screen. It displays a nice message stating the version of screen and that it's under the GPL.
Run the actual command.
At any time, hit ctrl-A, D to disconnect from the screen session. You'll see a message along the lines of [detached from pid.tty.server]. Log out from the server normally.
Connect to the server again and enter the command screen -x to reconnect to your session.

Resources