150

Is there a way to execute an ssh command when debugging a project with .vscode/launch.json?

For example: ssh -i xxxxx.

Or is it possible to create a command that you can run from the F1 command palette pop-up? Something like RunCustomCommandxx.

1

8 Answers 8

171

You can define a task in your tasks.json file and specify that as the preLaunchTask in your launch.json and the task will be executed before debugging begins.

Example:

In tasks.json:

For version 0.1.0:

{ "version": "0.1.0", "tasks": [{ "taskName": "echotest", "command": "echo", // Could be any other shell command "args": ["test"], "isShellCommand": true }] } 

For version 2.0.0 (newer and recommended):

{ "version": "2.0.0", "tasks": [{ "label": "echotest", "command": "echo", // Could be any other shell command "args": ["test"], "type": "shell" }] } 

In launch.json:

{ "configurations": [ { // ... "preLaunchTask": "echotest", // The name of the task defined above // ... } ] } 

Tasks documentation: https://code.visualstudio.com/docs/editor/tasks

Launch configuration: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Saravana, thanks for the response, Im getting a error: Failed to launch external program ssh -i ~/.ssh/some_key -L 3307:127.0.0.1:3306 [email protected] . spawn ssh -i ~/.ssh/some_key -L 3307:127.0.0.1:3306 [email protected] ENOENT
Make sure ssh is in your path. Or give the absolute path in your command.
@user3466947, isn't Address already in use saying you enough? Connect to the machine with ssh and try netstat -lnpt to see if the process is already using the address.
Which type of launch configuration do you use? The ones I have (e.g., python) require a "program" path...I guess I could just set it to a dummy python file. But, I don't really even want to engage the debugger, just run my task.
I believe this is out-of-date, now you can use type: node-terminal as described below.
|
54

The format changed. Visual Studio Code can create the tasks.json file for you. Inside your launch.json file and inside any configurations object, just define preLaunchTask to force auto-creation of the tasks.json template file:

{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "launch program", "skipFiles": [ "<node_internals>/**"], "preLaunchTask": "myShellCommand", "program": "${workspaceFolder}/test.js" } ] } 

If you do not have file tasks.json configured:

  • launch the debugger. Visual Studio Code will inform you: "could not find the task myShellCommand"
  • select Configure TaskCreate tasks.json file from templateOthers

Your tasks.json template file will then be created for you. Add your command to command and the name you put in preLaunchTask to label:

{ "version": "2.0.0", "tasks": [ { "label": "myShellCommand", "type": "shell", "command": "echo goodfood" } ] } 

2 Comments

in launch.json of .vscode: can I remove the program key because I just want to run the prelaunchtask as described?
Any idea how I could do this without installing node? Seems heavy handed when I just need to run a shell script. I get the error "Can't find Node.js binary "node": path does not exist."
26

Yes, it's possible. Just use:

"type": "node-terminal" 

(don't worry it says 'node-terminal', it should work out-of-the-box in VS Code)

and specify your command in "command" property:

"command": "ssh -i xxxx" 

Hope it helps.

5 Comments

This is the most elegant way out of all the other solutions IMO
I can't get rid of a prompt "... is already running" when starting a program second time. Has to close the terminal from each start, before running the next one.
@Basilevs I ran into the same issue. It seems like node-terminal opens a new, interactive shell session and pastes the command into it, and then waits for the shell session to end. The solution is to add ; exit to the end of your command, e.g. "command": "ssh -i xxxx; exit".
Or use && exit if you want to keep the terminal around if the command failed, so you can see the output.
this is the solution for me to run gunicorn commadline.
18
{ "version": "0.2.0", "configurations": [ { "name": "Launch app", "program": "lib/main.dart", "request": "launch", "type": "dart" }, { "name": "Build an APK Release", "command": "flutter build apk --release", "request": "launch", "type": "node-terminal" }, { "name": "Install an APK on a device", "command": "flutter install", "request": "launch", "type": "node-terminal" } ] } 

1 Comment

Just adding on to say that the "node-terminal" part can be used for any shell command, and the "node" part seems to be entirely irrelevant.
16

For me, I just needed an environment variable, which is different. You don't want a task for this because (at least for me) it doesn't work when the launcher is run.

Thanks to here, I got it working like this, inside my launcher (launch.json) entry:

"environment": [{ "name": "ENVIRONMENT_VARIABLE_NAME", "value": "${workspaceFolder}/lib" //Set to whatever value you want. }], 

Comments

6

My version of the configuration allows to just run a defined task and carried on (in my case, the task is to run the currently open Groovy file):

"configurations": [ { "name": "Launch groovy", "request": "launch", "type": "coreclr", "preLaunchTask": "groovy", "program": "cmd.exe", "args": ["/c"], "cwd": "${workspaceFolder}", "console": "internalConsole", "internalConsoleOptions": "neverOpen" } ] 

And the task:

"tasks": [ { "label": "groovy", "type": "shell", "command": "groovy ${file}" } ] 

1 Comment

I did this, and my tasks command is ran, however it does not carry on to launch the python file debugging. Any idea why?
2

To create a launch configuration, use the PowerShell Visual Studio extension and place your commands in the script.

launch.json

{ "version": "0.2.0", "configurations": [ { "name": "Build and run", "type": "PowerShell", "request": "launch", "script": " ${workspaceFolder}\\launch.ps1", } ] } 

launch.ps1

Start-Process -FilePath ssh -ArgumentList "-i","xxxxx" -NoNewWindow 

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

for flutter developers who is searching how to run flutter build commands. in tasks.json add

"tasks": [ { "type": "flutter", "command": "flutter", "args": [ "pub", "run", "build_runner", "build", "--delete-conflicting-outputs" ], "problemMatcher": [ "$dart-build_runner" ], "group": "build", "label": "flutter: flutter pub run build_runner build --delete-conflicting-outputs" }, ] 

then from vscode you can try "run task" it will show you flutter: flutter pub run build_runner build --delete-conflicting-outputs

this way you don't need to memorize and type to terminal source code generation/build commands

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.