I would like to use AppleScript to batch run a bunch of programs and get the time that each one ran.
Example: From the command line I would type time ls like this:
$ time ls
Applications
bin
net
Library
... etc
real 0m0.013s
user 0m0.002s
sys 0m0.006s
but when I try to use apple script and put the results into a variable, it completely ignores the time output.
Example:
set result to do shell script "time ls"
returns
Applications
bin
net
Library
... etc
with no mention of the time execution time anywhere.
Even making this more simple:
set result to do shell script "time"
returns
""
How do I get the time of execution back from AppleScript?
p.s. I'm not actually trying to time ls. I want to time a bunch of custom created programs, but ls makes simple example.
Use the exec 2>&1 command to redirect the STDERR to STDOUT, like this:
do shell script "exec 2>&1; time ls"
Related
I want to print the date after every bash command I run.
This could help me understand how much a command took to execute when I am away from keyboard.
I know I could do
`DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`
to get the date but I don't know how or even if it could be possible to run this command after every command I execute on bash.
I would also be interested in running the same command before every command so I could know how long a command took.
Is it possible?
What file should I edit?
For example:
$ wget google.com
15/07/2017 23:40:05
I would be happy, if I could also introduce this following feauture:
$ wget google.com
15/07/2017 23:40:05 15/07/2017 23:40:11
Program run for 00:00:06
where the first date is when I ran the program, second is when program terminated the third is self-explanatonary.
As you understood, I don't want to type every time
$ wget google.com && `DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`
To execute a cmd before every command entered, set a trap on DEBUG. Eg.
trap date DEBUG
To execute that command before emitting a prompt, set PROMPT_COMMAND:
PROMPT_COMMAND=date
This does exactly that:
PROMPT_COMMAND+=$'\n'"date +%d/%m/%Y\ %H:%M:%S"
The string in PROMPT_COMMAND gets evaluated after every command. You just need to add the date command to whatever you already had in it. ($'\n' (newline) is a somewhat more robust joiner than ; as two consecutive ; would give you a syntax error)
You can add date/time to your prompt, via PS1 variable. You could use date command, but it's more efficient to use the supported special characters, like \d for date, or \D{strftime-fmt}.
For example:
PS1='\u#\h[\D{%F} \D{%T}]\w\$ '
or, with color:
PS1='\[\033[01;32m\]\u#\h\[\033[00m\][\[\033[02;33m\]\D{%F}\[\033[08m\]T\[\033[00m\]\[\033[02;33m\]\D{%T}\[\033[00m\]]\[\033[01;34m\]\w\[\033[00m\]\$ '
will show:
user#host[2017-07-16 00:01:17]~/somedir$
Note that in the second case (with color) we have a valid ISO8601 timestamp, with a "hidden" date/time separator T in the middle. If you select it with a mouse, T is visible and can be copied. (Also double-click will select the complete timestamp, not only date or time.)
To print timestamp after every command just modify your PS1 prompt and add date to it. The only catch here is that it will tell you time when command ended and new prompt showed. So in case you have your prompt open for long time just hit enter to capture start time before running your command.
PS1="\D{%F %T} \$ "
See this arch wiki page or just google bash prompt customization.
To add time spent executing program just add time before the command
$ time wget google.com
It will give you output like this
real 0m0.177s
user 0m0.156s
sys 0m0.020s
And you can get even more lazy and for commands that you dont't feel like typing time every time you run it, just create alias.
alias wget="time wget"
Because in bash aliases are run before other commands you can do it this way even if it looks like recursion. Then you will call it as you are used to.
And of course, aliases and prompt settings can be put in your .bashrc file, so you don't have to type them every time you open terminal.
So, i'm running moses machine translation system on my server computer. I access terminal from ssh, and i came across an interesting problem.
The scrip i'm running uses > to specify and output file and it looks like this:
~/mosesdecoder/bin/moses -f /home/tin/working/filtered/moses.ini -i /home/tin/working/filtered/input.29242 > final
Now, since it will take some time for the translation to finish (around 10 hours) i want it to run with nohup, but when i do that even if i put & at the end i end up with file named "final" filled with stdout stuff.
Any idea on how to avoid it??
If you're running the commands inside an actual script file, you could get rid of the > inside the script, and run nohup ./sciptname.sh.
This will print the script's output to terminal, but nohup will redirect it to "nohup.out" in the current directory.
Source:
According to the nohup manpage I am reading, If the standard output is a terminal, the standard output is appended to the file nohup.out in the current directory.
Give it a shot :)
Is there any commands/parameters, which allow me to know for how long the process has been working? I tried to search something in commands ps and top , but didn't find it.
For a process whose time you want to (or accept to) collect after it ends, simply use the time command. It's available as an independent command and usually as a shell builtin as well.
$ time sleep 5
real 0m5.028s
user 0m0.000s
sys 0m0.000s
For a process that's still running, have a look at ps's formatting options.
$ ps -o time,etime 1
Exact option name might vary with the system, be sure to check your ps manual.
I'm working on a simple AppleScript that runs the following...
do shell script "diskutil partitionDisk /dev/disk1 1 APM JHFS+ Test 1G"
It's part of a bigger script. The above works fine but is there a way of viewing the progress? Anything will do really, preferably the actual command within a terminal window would be good.
Thanks
I often have the need for seeing the progress of do shell script commands and other various stages of an AppleScript script while it is running. What I do for do shell script is log the output to a log file and then use a program called MKConsole to display it on the Desktop. For example:
do shell script "diskutil verifyPermissions // &> /output.log"
In the above command diskutil will start up and start running and log standard output to the log file I created called output.log located at /. It won't show you the 0%...10%...20% progress indicator that you see in the Terminal but it does show you all the other output.
Then I configure MKConsole's preferences to read this log file and then in real time it will display all new log messages from that file on my Desktop.
If you need progress of non-shell script commands and need to log AppleScript activity then you can use the logger command. For example:
do shell script "logger -f /output.log The current count is: " & some_variable
If you have an AppleScript which runs for a long time and uses repeat loops and you'd like to know what stage of completion the script is in while running then
the above logger command is a great method for getting some feedback on the progress. I usually throw in a logger command right at the bottom of the script above the end repeat so that it is the last thing it does in that iteration of the loop.
The logger command method works well with MKConsole as well so that you can see the output in real time on your Desktop.
Invoke a terminal and have the terminal run diskutil:
"xterm -e 'diskutil partitionDisk /dev/disk1 1 APM JHFS+ Test 1G'"
is the commandline to pass to the shell, and have xterm(1) display the diskutil output. Other terminals(than xterm) can be used, and the window size font, and colors can be controlled: see the manual page for the terminal.
Maybe another way to show progress would be to run the entire script(not just the diskutil part) in a window.
How can one loop a command/program in a Unix shell without writing the loop into a script or other application.
For example, I wrote a script that outputs a light sensor value but I'm still testing it right now so I want it run it in a loop by running the executable repeatedly.
Maybe I'd also like to just run "ls" or "df" in a loop. I know I can do this easily in a few lines of bash code, but being able to type a command in the terminal for any given set of command would be just as useful to me.
You can write the exact same loop you would write in a shell script by writing it in one line putting semicolons instead of returns, like in
for NAME [in LIST ]; do COMMANDS; done
At that point you could write a shell script called, for example, repeat that, given a command, runs it N times, by simpling changing COMMANDS with $1 .
I recommend the use of "watch", it just do exactly what you want, and it cleans the terminal before each execution of the commands, so it's easy to monitor changes.
You probably have it already, just try watch ls or watch ./my_script.sh. You can even control how much time to wait between each execution, in seconds, with the -n option, and you can use -d to highlight the difference in the output of consecutive runs.
Try:
Run ls each second:
watch -n 1 ls
Run my_script.sh each 3 seconds, and highlight differences:
watch -n 3 -d ./my_script.sh
watch program man page:
http://linux.die.net/man/1/watch
This doesn't exactly answer your question, but I felt it was relavent. One of the great things with shell looping is that some commands return lists of items. Of course that is obvious, but a something you can do using the for loop is execute a command on that list of items.
for $file in `find . -name *.wma`; do cp $file ./new/location/ done;
You can get creative and do some very powerful stuff.
Aside from accepting arguments, anything you can do in a script can be done on the command line. Earlier I typed this directly in to bash to watch a directory fill up as I transferred files:
while sleep 5s
do
ls photos
end