How can cmd.exe be automatically closed after execution? - delphi

I run cmd.exe to move a file with Administrator rights:
ThisParams := '/K move ' + '"' + ThisSourceFile + '"' + ' ' + '"' + ATargetFile + '"';
Winapi.ShellAPI.ShellExecute(0, 'runas', 'cmd.exe', PChar(ThisParams), '', Winapi.Windows.SW_HIDE);
However, the cmd.exe process (although invisible) after execution remains active and in memory and stays visible in Task Manager.
How can cmd.exe, in this case, be automatically closed after execution?

As documented /k makes the command interpreter to continue running after executing the passed command. You should instead use
/c   Carries out the command specified by String and then stops.

Related

Unable to run PowerShell Write-EventLog within a DOCKERFILE

In my DOCKERFILE I'm running some PowerShell script which creates a custom event log (on Windows Server), then writes an entry. [I should add that I'm currently doing this only for debug purposes.]
The script/command which creates the event log appear to execute without any problems, but an exception is thrown when writing to the log using...
RUN powershell.exe -command Write-EventLog -LogName "my_log_name" -Source "my_source" -Message "EventLog created by DOCKERFILE." -Category 0 -EventID 0 -EntryType Information
The following exception is thrown...
Write-EventLog : A positional parameter cannot be found that accepts argument
'created'.
At line:1 char:1
+ Write-EventLog -LogName my_log_name -Source my_source -EntryType Messag ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Write-EventLog], Parameter
BindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
.Commands.WriteEventLogCommand
I also tried wrapping "EventLog created by DOCKERFILE." in parentheses but then it complained about the word by.
Why does it seem unable to parse "EventLog created by DOCKERFILE." as a string argument, but is instead parsing the words within the string?
UPDATE
Even if I wrap the command in double-quotes and the individual strings in single quotes, I get the same error.
UPDATE 2
Removing the quotes and escaping the spaces doesn't work either.
You need to use ` to escape the spaces:
RUN powershell.exe -command Write-EventLog -LogName my_log_name -Source my_source -Message EventLog` created` by` DOCKERFILE. -Category 0 -EventID 0 -EntryType Information
See: https://docs.docker.com/engine/reference/builder/#escape
This works for me from cmd (except for the unknown source). I'm not sure if docker has issues with single quotes. Double quotes are special in cmd. Note that new-winevent has replaced write-eventlog.
powershell write-eventLog my_log_name my_source 0 information 'EventLog created by DOCKERFILE.' -category 0

How to get input from user and use it in CMD using DXL?

Hello I have been trying to get user input and store it in a variable, and then use that variable in a CMD. ALL of this in a DXL script!
Take a glance into the code I have tried this but still not working.
string wheretosave = ""
DB exBox = create "Get Path"
DBE stringInn = field(exBox, "ADD Path where to save:", "", 80)
void doGet(DB exBox) {
wheretosave = get stringInn
print wheretosave "\n"
system("cmd.exe /C cd /d wheretosave & dir & PAUSE")
}
apply(exBox, "Get", doGet)
show exBox
to add "wheretosave" to a string, use
system("cmd.exe /C cd /d " wheretosave " & dir & PAUSE")
But I think the system call does not work. When I start a command line and do a command
cmd.exe /C cd /d d:\temp & dir & PAUSE
then I get the dir of the original directory.
#root: ADDITION: my problem was that I did not have a directory d:\temp on my PC...
But after reading your comment, it seems the point was not understood.
To be more precise:
First: there is no interpolation in DXL. If you want to create a string which contains a) some fixed characters, b) the content of a variable (here: wheretosave) and c) some more fixed characters, you have to use a < space > to concanate the three parts. So, do it like this: string s1 = "fixedtexta" wheretosave "fixedtextb".
Second: If the fixed characters contains a quotation mark then you have to escape it with a backslash. So, this example would become to string s2 = "fixedtexta\"" wheretosave "\"fixedtextb" to get fixedtexta"hello world"fixedtextb if wheretosavecontains hello world
BUT your example is even more difficult.
Third: cmd.exe /C takes only one parameter. So, if you want to do more than one command in the cmd subshell, you have to surround all the commands with enclosing quotation marks cmd.exe /C "cmd 1 & cmd 2 & cmd 3" The way you wrote it would have translated to
create a subshell which does a "cd /d", end the subshell
in the main shell (which is still in the original directory), do a "dir"
in the main shell, do a "pause"
Fourth: In DOS, if you have a quotation mark inside a string, you have to escape it with a second quotation mark.
All in all the command you are looking for is
string wheretosave = "d:\\temp x" // or get stringInn in your example
system("cmd.exe /C \"cd /d \"\"" wheretosave "\"\" & dir & pause\"")
Try providing the full path and apply Mike's correction. Using "C:\Windows" works as expected (for me): the cmd shows the content of my Windows folder on drive C.

io.popen how to get the go output in lua

How could I capture compiler errors go in lua?
I'm trying to get the output of the comp compiler errors in a tmux panel using lua
when executing the script the result is only shown in the current panel and not in the second panel
and the /tmp/output file is always empty
cmd=io.popen("go build -gcflags=-e scree.go")
f=io.open("/tmp/output")
f:write(cmd:read("*all"))
for line in f:lines() do
os.execute("tmux run-shell -t 2 'echo " .. line .. "' ")
end
f:close()
Is there any way to do this without using a temporary file?
I'm not totally clear on this. But maybe something like the following. i.e. pipe stderr to stdout and capture the result (not tested).
f = assert (io.popen ("go build -gcflags=-e scree.go 2>&1"))
for line in f:lines() do
os.execute("tmux run-shell -t 2 'echo " .. line .. "' ")
end
f:close()
I think the key is that popen won't capture stderr. See further details about that here

Bind key prefix + ; to open command-prompt in tmux

Normal way to open the command-prompt in tmux is prefix + :. I want to bind the sequence prefix + ; to open the command prompt. I am too lazy to hit the shift key.
When I put this in my tmux.conf: bind-key ; command-prompt, I get this error: /Users/skilbjo/.tmux.conf:19: usage: bind-key [-cnr] [-t mode-table] [-T key-table] key command [arguments]
which is funny, because when I do prefix + ? (alias for tmux list-keys), this is listed: bind-key -T prefix : command-prompt. How does this sorcery work? I even tried bind-key -T prefix ; command-prompt to no avail, same error message
tmux uses semicolon as a command separator.
From the tmux man page:
Multiple commands may be specified together as part of a command sequence. Each command should be separated by spaces and a semicolon; commands are executed sequentially from left to right and lines ending with a backslash continue on to the next line, except when escaped by another backslash. A literal semicolon may be included by escaping it with a backslash (for example, when specifying a command sequence to bind-key).
What you'll want to do is:
unbind-key \;
bind-key \; command-prompt

Run a loop in bash with timeout in a single line

I need to have a single line command that does something in a loop with a timeout.
Something like
export TIMEOUT=60
export BLOCK_SIZE=65536
COMMAND="timeout TIMEOUT while true; do dd if=/tmp/nfsometer_trace/mnt/TESTFILE of=/dev/null bs=BLOCK_SIZE; done"
The command will be executed in 'sh' by doing the following:
echo $COMMAND > COMMAND_FILE
sh COMMAND_FILE
But this gives me a syntax error:
syntax error near unexpected token `do'
Is there any way to have a single line command to timeout an infinite loop?
A command like
while true ; do echo $(( i++ )) ; sleep 5 ; done
will produce output like
0
1
2
3
But if I try to use that command directly in timeout, I get similar error messages, i.e.
timeout 20 while true ; do echo $(( i++ )) ; sleep 5 ; done
bash: syntax error near unexpected token do
Some programs (ssh for example), will process long strings of logic, if quoted so the argument is all one string.
timeout 20 "while true ; do echo $(( i++ )) ; sleep 5 ; done"
timeout: failed to run command 'while true ; do ...'
same error msg whether I use single of dbl-quotes to pass in string.
Reading info timeout we see that
COMMAND must not be a special built-in utility (*note Special built in utilities::).
Maybe while loops qualify as special built-in utility?
Finally, note that I have removed worrying about does using the "wrapper" information you have, ie.
export TIMEOUT=60
export BLOCK_SIZE=65536
COMMAND="timeout TIMEOUT while ....
as an cause of your problem.
When you have a problem like this, it is better to prove to yourself that your command is working in a simpler case and then get it work inside a more complex usage.
Given this evidence, I don't think timeout is designed to do what you want.
As a last resort, I recommend that you try converting that while loop into a script and calling just
timeout 20 myLooperScript
IHTH

Resources