AutoHotKey run command in background - autohotkey

I'm trying to run a command with AutoHotKey that I would normally run with cmd.exe. Here is the command:
pandoc -s "C:\input.txt" -o "D:\output.html"
This is how I do it in AutoHotKey:
#a::
run pandoc -s "C:\input.txt" -o "D:\output.html"
return
The only problem is that this opens up the a command prompt called "pandoc". Normally I'd just type in the command in cmd.exe and it would run without any hiccups or any windows opening. For this, however, that pandoc window shows up. Am I doing it correctly? Is there any easy way to suppress the window and run the command in the background?

Runs a program without opening a window. The program is "cmd.exe", the windows command shell. It is invoked with arguments "/c time /t", which outputs the current time. It redirects the output to "c:\t.txt"
program
#a::
run cmd /c time /t > c:\t.txt, c:\, hide
return
output
c:\>type c:\t.txt
14:28

Related

Create a task with task Scheduler to run cmd.exe with commands

I created a task with the task scheduler in windows 10 to open cmd.exe. it ran successfully like this
But I need to run some commands every time it opens something like this
and automatically runs this command.
this is important that it opens a command prompt first and then runs the command inside the command prompt automatically.
Thanks.
Use /k flag. See cmd.exe /k switch.
If you don't need console window to stay after commands completed use /c flag. You can read more about them with cmd /?

Windows terminal: open multiple panes and execute specified command

I recently downloaded the new Windows Terminal. I have created the shortcut for opening the multiple panes(which is working fine). However, I am trying to execute a command for the respective pane.
wt -d <path> -c "cls && php artisan serve" ;
split-pane -p "Command Prompt" -H -d <path> -c "npm run watch"
I googled for the solution but no luck.
Is this even possible..?
I got a similar setup working.
Im running Windows Terminal version 1.8.1444.0
My goal was to setup a dotnet core app running in left pane and a react-app running in right pane:
wt -d "C:\path\to\dotnetcoreapp" -p "Command Promt" cmd /k dotnet watch run ; split-pane -d "C:\path\to\reactapp" cmd /k yarn start
Also tried to start an interactive elixir session: wt -d "C:\dev\elixir" cmd /k IEx which also worked fine...
The short answer is: Yes it is possible but it is a workaround.
The Challenges
wt.exe does not currently have a command line option to execute a
command from a split-pane
wsl.exe (which runs your default shell such as bash) does not currently support opening a shell with a command without exiting the shell right after the command is run.
The workaround
To get around the first challenge we can launch a custom profile that executes the command via wsl.exe in the key value pair (in settings json) "commandline": "wsl.exe 'commands go here"
To get around the second challenge we need to execute the wsl.exe 'commands go here' via powershell.exe because Powershell has a -NoExit option which will keep the shell open after the command is executed. So for example if you wanted to open a shell that runs wsl.exe (your linux shell) with the command watch ps then the line in the custom profile would look like this:
"commandline": "powershell.exe -NoExit -Command wsl.exe watch ps"
The Solution:
Create a profile in Windows Terminal settings.json for each command you want to run. Each profile should have a unique guid that you can generate in Powershell by running the command [guid]::NewGuid(). So the profile to run the command watch ps would look something like this:
{
"guid": "{b7041a85-5613-43c0-be35-92d19002404f}",
"name": "watch_ps",
"commandline": "powershell.exe -NoExit -Command wsl.exe watch ps",
"hidden": false,
"colorScheme": "One Half Dark"
},
Now you can open a tab in windows terminal with two panes, the pane on the right will run the command watch ps and the shell will stay open. Put something like the below line of code in your shortcut (or from the command line) where the value of the option -p is equal to the value of the profile you created. Each additional pane you open will need a profile that has the command you want to run in it.
wt split-pane -p "watch_ps"

Notepad++ multiple run commands

How can I run cd "$(CURRENT_DIRECTORY)" followed by path/to/python.exe -i "$(FILE_NAME)" in the notepad++ run dialogue (F5) please?
I don't want to use nppexec because I want the program to run in the windows cmd window not the npp console. Also I specifically need to cd into the folder rather than relying on the default behaviour. I've tried separating the commands with semi-colons and commas, but no joy.
In the DOS/Windows command shell, you separate multiple commands on a single line using the ampersand character (&):
cmd /c cd \ptools & dir /s /b *.awk & pause
This changes to the \ptools directory, then shows every AWK file and waits for a key to be pressed.
In your case:
cmd /c cd "$(CURRENT_DIRECTORY)" & path\to\python.exe -i "$(FILE_NAME)"

Unable to open a second command window from batch file

I have following command to start after other which does not depend on each other. However, with following command, I only can start #1, #2. My command #3 does not start .
I would like to know if i have make any mistake. I did not see the the third line executed.
// make a directory
// startd adb logcat
// shell adb monkey
"test.bat"
mkdir c:\test_log_file
START "powershell Window" powershell adb logcat >> "C:\Testdata.txt"
START "Powershell Window 2" shell adb monkey -p com.android.browser
Based on your earlier question (how to open power shell window from a batch file to a new window), I'm assuming that "shell" is a command you have available to you, and not a typo. Two possibilities come to mind:
Is "shell" in the path? If not, you need to provide the full path to this command. (Probably not the answer, because if this were the issue you'd get a popup error message telling you that Windows can't find "shell".)
Although you've titled the window "Powershell Window 2" in your second START command, this command should actually open a cmd window, not a powershell window. Are you looking for a new powershell window, when you actually have a cmd window that's titled "Powershell Window 2"?
If you want to start a powershell window, the command should be:
START "Powershell Window 2" powershell -noexit shell adb monkey -p com.android.browser

Cygwin - run script silenty from "run command"

I have script lets say:
C:\foo.bsh
I want to be able to run this command via the windows run command:
Start -> Run
Windows Key + R
and type something small like 'foo' and hitting return.
However, I do not want a cmd prompt to be visible. This script does some preprocessing for an IDE. I do not want the cmd prompt to be open for the lifetime of the IDE process.
I have tried:
1) Creating a bat file with the following contents:
c:\cygwin\bin\bash --login "C:\foo.bsh" (this fails because it keeps a cmd open)
2) Converting the above bat file to an exe using bat_2_exe_converter (does not make the cmd silent)
thoughts?
EDIT: The solution so far suggests something to type from an actual cygwin shell. I am trying to get a faster solution by having something short I can type in the Windows run command. Also, the nohup command; exit doesn't automatically kill the box - however I can manually kill it without killing the IDE process. The run command accepts shortcuts (.lnk's), bat's, exe's.
Try the run.exe command of cygwin. It is a big install, a complete unix environment for your Windows machine. Suppose you installed it at c:\cygwin\.
No mystery, just run c:\cygwin\bin\run.exe <your command here> and you will have your no dos window execution.
You can run it from any DOS window (run cmd.exe from the start menu). You don't need to run it from cygwin.
To make it easier, append C:\cygwin\bin to your %PATH% env var (My Computer → Properties → Advanced → Environment Variables) (Kudos to Felipe Alvarez comment).
Now you can just type
c:\cygwin\bin\run.exe "C:\foo.bsh"
You must create a link in your Start Menu with this command so will be able to run it with Win-R.
Here is the man page of the runcommand:
$ man run
RUN(1) run 1.3.0 RUN(1)
NAME
run - start programs with hidden console window
SYNOPSIS
run [ -p path ] command [ -wait ] arguments
runcommand [ -p path ] [ -wait ] arguments
DESCRIPTION
Windows programs are either GUI programs or console programs. When
started console programs will either attach to an existing console
or create a new one. GUI programs can never attach to an exiting con‐
sole. There is no way to attach to an existing console but hide it if
started as GUI program.
run will do this for you. It works as intermediate and starts a pro‐
gram but makes the console window hidden.
With -p path you can add path to the PATH environment variable.
Issuing -wait as first program argument will make run wait for program
completition, otherwise it returns immediately.
The second variant is for creating wrappers. If the executable is
named runcommand (eg runemacs), run will try to start the program (eg
emacs).
EXAMPLES
run -p /usr/X11R6/bin xterm
run emacs -wait
runemacs -wait
run make -wait
AUTHORS
Charles S. Wilson
Harold L Hunt II
Jehan Bing
Alexander Gottwald
Version 1.3.0 November 2005 RUN(1)
You can use either...
c:\cygwin\bin\bash -l /path/to/script_to_interpret.sh
...or...
c:\cygwin\bin\bash -l -c /path/to/executable_script.sh
Note: the -l flag tell bash to "act as if it had been directly invoked by login" and use Bash Startup Files. This is important in that it sets your $PATH and other things you rely on when you launch a cygwin terminal. If you don't include -l or --login you will get "command not found" when you try to call anything except of a bash builtin.
The difference between the 2 is like the difference between doing...
bash script_to_interpret.sh
...and...
./executable_script.sh
...in *nix. The former interprets the script using bash. The latter executes the script (only if it has chmod +x executable_script.sh) and interprets it according to its "shebang" line. The latter method is also what you want to do if your executable is not a script at all, like a *nix binary compiled from source.)
It has been bugging me for a while I couldn't find the solution for this, but I finally got the right mix together.
You can simply do the following if you have cygwin on your PATH:
run bash test.js
If cygwin is not on your path, you can do this:
c:\cygwin\bin\run.exe -p /bin bash test.js
If you are looking for more control over the created window (maximize, etc) it looks like you can use cygstart also.
Sources:
- neves answer above (though that wasn't enough by itself for me personally to figure it out)
- http://cygwin.com/ml/cygwin/2008-09/msg00156.html
As the terminal can't close while your script is still running, try the command:
"nohup C:\foo.bsh; exit"
This way your script will be backgrounded and detached from the terminal, and it should exit quickly so the terminal goes away. I think that the window may still 'flash' with this approach, but the results should be better than what you're getting.
I'm running Cygwin64 and the xwin server link points to:
C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
This creates an icon AND a notification on the taskbar. I don't like that. The icon is rather useless, the notification has all your menu options from .XWinrc.
So... I wrote a .vbs script to silently run this command and make the icon go away:
Set objShell = CreateObject("WScript.Shell")
objShell.Run("C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe"), 0
Another imperfect possibility is to run the script via a shortcut and set the shortcut's Run option to "minimized".
Go to the directory where you have installed cygwin(on my machine it is c:/cygwin64/bin)
Once there simply type "bash.exe"