i have a batch file which helps to start my rails server.when i am starting my batch file the command prompt is opening but here i need the cmd should not visible to user or it will execute at windows background.I am explaining mt .bat file code below.
c:
cd c:\\Site\swargadwara_puri
rails server
Please help me.
You could run it silently using a Vbscript file instead. The Run Method allows you running a script in invisible mode. Create a .vbs file like this one :
Option Explicit
Dim MyBatchFile
MyBatchFile = "C:\New Floder\toto 1.bat"
Call Run(MyBatchFile,1,False) 'Showing the console
Call Run(MyBatchFile,0,False) 'Hidding the console
'*********************************************************************************
Function Run(MyBatchFile,Console,bWaitOnReturn)
Dim ws,Result
Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the MS-DOS console
If Console = 0 Then
Result = ws.run(DblQuote(MyBatchFile),Console,bWaitOnReturn)
If Result = 0 Then
'MsgBox "Success"
Else
MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
End If
End If
'A value of 1 to show the MS-DOS console
If Console = 1 Then
Result = ws.run(DblQuote(MyBatchFile),Console,bWaitOnReturn)
If Result = 0 Then
'MsgBox "Success"
Else
MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
End If
End If
Run = Result
End Function
'*********************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************
The second argument in this example sets the window style. 0 means "hide the window, and 1 means "show the window"
Complete syntax of the Run method:
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Arguments:
object: WshShell object.
strCommand: String value indicating the command line you want to run. You must include any parameters you want to pass to the executable file.
intWindowStyle: Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.
bWaitOnReturn: Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).
You can minimize the batch command, for example using:
START /MIN rails server
Related
I'm trying to run a series of tests and collect some meta data on each test. If there is an error during one of the tests, I would like to save the back trace information but not to exit the script. For example:
-- Example program
for _, v in ipairs(tests) do
--check some results of function calls
if v == nil then
--error("function X failed") no exit
--save back trace to variable/file
-- continue with program
end
end
I'm not currently aware if it is possible in lua to tell the function error()
not to stop after creating the back trace. Any thoughts on how to do this?
debug.traceback ([thread,] [message [, level]]) (source) is what you're looking for. You can write a function that 1. gets a traceback 2. opens a file 3. writes the traceback to the file 4. closes the file.
In that case you'd have to use a level of 2, since 0 would be the debug.traceback function, 1 would be the function calling it (i.e. your function) and 2 the funcion calling that one. message could be your error code. Then you just override the error function locally in your script and you're done; calling error will just log the error and not exit the program.
EDIT: You can also override error globally, if you want, but that might lead to unexpected results if something goes terribly wrong somewhere else (code that you didn't write yourself) and the program continues nonetheless.
You'd be better off with a construct like this:
if os.getenv 'DEBUG' then
my_error = function()
-- what I explained above
end
else
my_error = error
end
and just use my_error in all the places where you'd usually use error.
when I use Lua script to query a list, I got the correctly result if the list is not empty. But got error if the list is empty.
Blow is my script:
const char * sLuaQueryServers = "local key_list = redis.call('KEYS',
KEYS[1]); return(redis.call('MGET', unpack(key_list)))";
I passed the "serverlist:*" as the key, it's successfully returned the server in list.
But if there no server in redis, I got below error:
ERR Error running script (call to
f_88620231033e13635dc3181f2947a740f91012dc): #user_script:1: #user_script:
1: Wrong number of args calling Redis command From Lua script
"
Please help.
To your question, add a check that the list isn't empty before calling MGET, e.g.:
local key_list = redis.call('KEYS', KEYS[1])
if #key_list > 0 then
return(redis.call('MGET', unpack(key_list)))
else
return nil
end
Note #1: no need for semicolons in Lua
Note #2: Using KEYS isn't recommended for anything, except debugging
Note #3: You're using the KEYS table to pass an argument, but since your script is running KEYS (the command) that's really a moot point
I am running my own proxy objects which extend org.mozilla.javascript.ScriptableObject.
I also have my own functions which extend org.mozilla.javascript.Function.
My desire is to have any exceptions thrown here return the line no and if possible the column number where they occurred in the evaluated script. Is this possible? I only have access to the context and the scope.
Whenever an exception is thrown from a script, Rhino throws RhinoException which already has line and column number (and more). However when you execute the script you need to provide the line number that will be used by Rhino as the starting line number. The actual line number of where the exception/error occurred will be relative to this number. So something line this:
//-- Define a simple test script to test if things are working or not.
String testScript = "function simpleJavascriptFunction() {" +
" this line has syntax error." +
"}" +
"simpleJavascriptFunction();";
//-- Compile the test script.
Script compiledScript = Context.getCurrentContext().compileString(testScript, "My Test Script", 2, null);
//-- Execute the test script.
compiledScript.exec(Context.getCurrentContext(), anyJavascriptScope);
In the above code, the starting line number is set to 2 (third parameter of the call to compileString()). When this is executed, Rhino will throw a RhinoException that will have the lineNumber property set to the value '3' (the first line is treated as the second line b/c we passed 2).
Hope this helps.
I need to use Lua to run a binary program that may write something in its stdout and also returns a status code (also known as "exit status").
I searched the web and couldn't find something that does what I need. However I found out that in Lua:
os.execute() returns the status code
io.popen() returns a file handler that can be used to read process output
However I need both. Writing a wrapper function that runs both functions behind the scene is not an option because of process overhead and possibly changes in result on consecutive runs. I need to write a function like this:
function run(binpath)
...
return output,exitcode
end
Does anyone has an idea how this problem can be solved?
PS. the target system rung Linux.
With Lua 5.2 I can do the following and it works
-- This will open the file
local file = io.popen('dmesg')
-- This will read all of the output, as always
local output = file:read('*all')
-- This will get a table with some return stuff
-- rc[1] will be true, false or nil
-- rc[3] will be the signal
local rc = {file:close()}
I hope this helps!
I can't use Lua 5.2, I use this helper function.
function execute_command(command)
local tmpfile = '/tmp/lua_execute_tmp_file'
local exit = os.execute(command .. ' > ' .. tmpfile .. ' 2> ' .. tmpfile .. '.err')
local stdout_file = io.open(tmpfile)
local stdout = stdout_file:read("*all")
local stderr_file = io.open(tmpfile .. '.err')
local stderr = stderr_file:read("*all")
stdout_file:close()
stderr_file:close()
return exit, stdout, stderr
end
This is how I do it.
local process = io.popen('command; echo $?') -- echo return code of last run command
local lastline
for line in process:lines() do
lastline = line
end
print(lastline) -- the return code is the last line of output
If the last line has fixed length you can read it directly using file:seek("end", -offset), offset should be the length of the last line in bytes.
This functionality is provided in C by pclose.
Upon successful return, pclose() shall return the termination status
of the command language interpreter.
The interpreter returns the termination status of its child.
But Lua doesn't do this right (io.close always returns true). I haven't dug into these threads but some people are complaining about this brain damage.
http://lua-users.org/lists/lua-l/2004-05/msg00005.html
http://lua-users.org/lists/lua-l/2011-02/msg00387.html
If you're running this code on Win32 or in a POSIX environment, you could try this Lua extension: http://code.google.com/p/lua-ex-api/
Alternatively, you could write a small shell script (assuming bash or similar is available) that:
executes the correct executable, capturing the exit code into a shell variable,
prints a newline and terminal character/string onto standard out
prints the shell variables value (the exit code) onto standard out
Then, capture all the output of io.popen and parse backward.
Full disclosure: I'm not a Lua developer.
yes , your are right that os.execute() has returns and it's very simple if you understand how to run your command with and with out lua
you also may want to know how many variables it returns , and it might take a while , but i think you can try
local a, b, c, d, e=os.execute(-what ever your command is-)
for my example a is an first returned argument , b is the second returned argument , and etc.. i think i answered your question right, based off of what you are asking.
Say I have want to execute a script or and executable file by printing runtime the output of execution.
When I do:
set log [exec ./executable_file]
puts $log
Then it waits a long time and then prints everything at once. But I want runtime printing. How can I do this?
Not perfect (as it require writing to external file):
set log [exec executable_file | tee log.txt >#stdout]
The output will be displayed immediately, at the same time, saved to 'log.txt'. If you don't care about saving the output:
set log [exec executable_file >#stdout]
Use open "| ..." and asyncronous linewise reading from the returned descriptor, like this:
proc ReadLine fd {
if {[gets $fd line] < 0} {
if {[chan eof $fd]} {
chan close $fd
set ::forever now
return
}
}
puts $line
}
set fd [open "| ./executable_file"]
chan configure $fd -blocking no
chan event $fd readable [list ReadLine $fd]
vwait forever
See this wiki page for more involved examples.
In a real program you will probably already have an event loop running so there would be no need for a vwait specific to reading the output of one command.
Also if you need to collect the output, not just [puts] each line after it has been read, you will pobably need to create a global (usually namespaced) variable, initialize it to "", pass its name as another argument to the callback procedure (ReadLine here) and append the line to that variable's value.
May be you can launch another process in background before your executable, like tail -f logfile.txt, and outputting the results of your executable in this logfile.txt ?