Skip to content

Instantly share code, notes, and snippets.

@gabor-m
Created January 14, 2022 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabor-m/a3f65f4b7cbe3dbc6a5f75bce3a04df8 to your computer and use it in GitHub Desktop.
Save gabor-m/a3f65f4b7cbe3dbc6a5f75bce3a04df8 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"log"
"strings"
"os/exec"
"syscall"
"regexp"
"path/filepath"
//"encoding/json"
)
// Taskkill /F /PID 11111
func runProcess(path string, str string) string {
cmd := exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: false,
CmdLine: ` /C START /B powershell.exe -Command "$startup=[wmiclass]'Win32_ProcessStartup';$startup.Properties['ShowWindow'].value=$False;([wmiclass]'win32_Process').create('` + str + `','` + path + `',$Startup);"`,
CreationFlags: 0,
}
var outbuf, errbuf strings.Builder // or bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
if err := cmd.Run(); err != nil {
return ""
}
stdout := outbuf.String()
var pattern = regexp.MustCompile(`ProcessId\s*:\s*([0-9]+)`)
PID := pattern.FindStringSubmatch(stdout)[1]
return PID
}
func main() {
// os.FindProcess(pid int) (*Process, error)
ex, _ := os.Executable()
exPath := filepath.Dir(ex)
argsWithProg := os.Args
log.Print(exPath)
log.Print(argsWithProg)
PID := runProcess(`C:\`, `php -S localhost:80`)
dirPath := filepath.Join(exPath, "pids")
os.Mkdir(dirPath, 0777)
//processPath = ;
os.WriteFile("a.txt", []byte("XXX"), 0777)
}
/*
exec run php:base64 cGhwIC1TIGxvY2FsaG/9zdDo1MDAw
exec start C:\www\myproject php -S localhost:80
exec stop {pid}
exec jobs
=> [{"pid":15,"path":"C:\","cmd":"php -S localhost"}]
exec cmds
=> []
exec add php8 C:\php8\php.exe
exec rm php
process
15165
1515
15656
commands
php7
php8
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment