Stéphane Ducasse wrote:
>
> can you give an example so that people can learn too?
>
Sure.
So I have a simple bash script that calls the vm, the image, and the script:
----------------bash script-----------
#!/bin/bash
export AN_ENVIRONMENT_VARIABLE=somevalue
export SCRIPT_LOG_FILE=afilename
"$pharovm_dir"/squeak -plugins "$pharovm_dir" -encoding Latin1 -headless
"$pharo_image" "$smalltalk_script" && exit 0
echo Something bad happened!
exit 1
----------------------------------------
the $smalltalk_script is an .st file with the code to be executed (an
example later).
environment variables, such as AN_ENVIRONMENT_VARIABLE and SCRIPT_LOG_FILE,
are useful to pass some info to the smalltalk script in an easy way, here we
use SCRIPT_LOG_FILE to save some script's output to a file.
Here I put a very simple example based on what I did:
----------------pharo script (aka $smalltalk_script)-----------
[| fs environment pid |
environment := OSProcess thisOSProcess environment.
pid := OSProcess thisOSProcess pid.
someValue := (environment at: 'AN_ENVIRONMENT_VARIABLE').
fs := FileStream forceNewFileNamed: (environment at: 'SCRIPT_LOG_FILE').
loggingBlock := [:message |
fs nextPutAll: TimeStamp now asString, ' [', pid asString, '] ' .
fs nextPutAll: message; nextPut: Character lf.].
loggingBlock value: 'Starting script with ', someValue.
"do some interesting stuff here"
"..."
Smalltalk snapshot: false andQuit: true.] on: Error do: [:err | OSProcess
thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf.
Smalltalk snapshot: false andQuit: true.]
------------------------------------------
A few comments:
- with "OSProcess thisOSProcess environment" you can access environment
variables as a dictionary (that's why I use them instead of command line
arguments)
- "OSProcess thisOSProcess pid" gives you the pid of your process, in this
way I can make a unix-like log file
- "Smalltalk snapshot: false andQuit: true." closes the image correctly at
the end of your script and does NOT save the state on the image.
- "[...] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll:
err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit:
true.]" is _VERY_ useful to debug your code. Any time there is an error in
the main block, it catches it and prints the message to the console using
the standard error. This is essential to be able not to spend ages in trying
to understand what went wrong with your script.
That's all. I hope it's useful to someone.
Cheers,
Alberto
--
View this message in context:
http://forum.world.st/Headless-PetitParser-tp3994446p4075649.html
Sent from the Pharo Smalltalk mailing list archive at Nabble.com.