Export/Stream Mac Terminal Output To File - macos

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 &

Related

Mac OSX Bash Script stop processing while specific window is open

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.

mac terminal login costs too much time

Every time I open a new terminal it costs about 3-5 seconds to login , plz see the screenshot.
Terminal seems to save every command to ASL (Apple System Log) log files and it can become cumbersome. Many reports that deleting ASL log files speed up the Terminal. Try:
sudo rm /private/var/log/asl/*.asl
and relaunch Terminal.

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.

How to capture and display output from a task via Windows CMD

I've got a PHP script which I'm running from a command line (windows) that performs a variety of tasks, and the only output it gives is via 'print' statements which output direct to screen.
What I want to do is capture this to a log file as well.
I know I can do:
php-cli script.php > log.txt
But the problem with this approach is that all the output is written to the log file, but I can't see how things are running in the mean time (so I can stop the process if anything dodgy is happening).
Just to pre-empt other possible questions, I can't change all the print's to a log statement as there are far too many of them and I'd rather not change anything in the code lest I be blamed for something going fubar. Plus there's the lack of time aspect as well. I also have to run this on a windows machine.
Thanks in advance :)
Edit: Thanks for the answers guys, in the end I went with the browser method because that was the easiest and quickest to set up, although I am convinced there is an actual answer to this problem somewhere.
You can create a powershell script that runs the command, reads the data from the command's STDOUT then outputs the output to both the log file and the terminal for you to watch. You can use the commands Write-Output and Write-Host.
Microsoft's site: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/tee-object.mspx
Another option would be use find a tee program that will read input and divert it to two different outputs. I believe I have seen these for windows but I don't think they are standard.
Wikipedia: http://en.wikipedia.org/wiki/Tee_(command)
I have always opened the log file up in my web browser. This allows me to refresh it easily and does not interrupt any writing to the file that windows does. It isn't particularly elegant but it does work!
You want the "tee" command for Windows. See http://en.wikipedia.org/wiki/Tee_(command)
Powershell includes a tee command, and there are also numerous versions of tee for Windows available, for instance:
http://unxutils.sourceforge.net/
http://www.chipstips.com/?p=129
Also can be implemented in VBScript if you prefer.
EDIT: Just occurred to me I should also mention the tail command: http://en.wikipedia.org/wiki/Tail_(Unix). Tail allows you to read the last N lines of a file, and also includes a "file monitor" mode that just continually displays the end of the file in real-time. This is perfect for log file monitoring since it allows you to watch the log in real-time without interfering with the process that's writing to the log. There are several implementations of tail for Windows, both command line and GUI based. Microsoft's Services For UNIX packages (or whatever they're calling it now) also include a version of tail. Some examples:
mTail
Tail for Win32
WinTail
MakeLogic Tail
Some of these go far beyond just displaying the file in real-time as it updates and can send email alerts and colorize string matches, monitor multiple files at once, etc.
Slow:
for /f "delims=" %a in ('php-cli script.php') do #echo %a&echo %a>>log.txt
or in a batch file:
for /f "delims=" %%a in ('php-cli script.php') do #echo %%a&echo %%a>>log.txt

Resources