There is no command similar to nohup under windows, but you can use other commands to achieve this function.
1. Explanation of commands used:
1. Start-Process: Start one or more processes on the local computer.
Related introduction links: Start-Process (Microsoft.PowerShell.Management) | Microsoft Learn?
Mainly used parameters
1.1, -WindowStyle
Specifies the state of the window used for new processes. Acceptable values for this parameter are: **Normal**, **Hidden**, **Minimize**, and **Maximize**. The default value is **Normal**.
1.2, -FilePath
Specifies an optional path and file name for the program running in the process. Enter the name of an executable file or document associated with the program on your computer, such as a .txt
or .doc
file. This parameter is required.
1.3, -RedirectStandardOutput
Specify the file. This cmdlet sends the output produced by the process to the specified file. Enter the path and file name. By default, this output is displayed in the console.
2. tasklist
Directly enter tasklist /? in powershell to view the help
TASKLIST [/S system [/U username [/P [password]]]] [/M [module] | /SVC | /V] [/FI filter] [/FO format] [/NH] describe: This tool displays a list of processes currently running on a local or remote machine. parameter list: /S system specifies the remote system to connect to. /U [domain\]user Specifies the user context in which this command should be executed. /P [password] Specifies the password for the provided user context. If omitted, then Prompt for input. /M [module] Lists all tasks currently using the given exe/dll name. If no module name is specified, all loaded modules are displayed. /SVC displays the services hosted in each process. /APPS Displays Microsoft Store apps and their associated processes. /V displays detailed task information. /FI filter displays a list of matching filters Tasks that specify conditions. /FO format specifies the output format. Valid values: "TABLE", "LIST", "CSV". /NH specifies that column headers should not be shown in the output. Only valid for "TABLE" and "CSV" formats. /? Display this help message. Filter: Filter name Valid operators Valid values ---------- --------------- ---------------------------- -- STATUS eq, ne RUNNING | SUSPENDED NOT RESPONDING | UNKNOWN IMAGENAME eq, ne image name PID eq, ne, gt, lt, ge, le PID value SESSION eq, ne, gt, lt, ge, le session number SESSIONNAME eq, ne session name CPUTIME eq, ne, gt, lt, ge, le CPU time, the format is hh:mm:ss. hh - hours, mm - minutes, ss - seconds MEMUSAGE eq, ne, gt, lt, ge, le Memory usage in KB USERNAME eq, ne username in the format [domain\]user SERVICES eq, ne service name WINDOWTITLE eq, ne window title module eq, ne DLL name NOTE: "WINDOWTITLE" and "STATUS" are not supported when querying remote computers filter. Examples: TASKLIST TASKLIST /M TASKLIST /V /FO CSV TASKLIST /SVC /FO LIST TASKLIST /APPS /FI "STATUS eq RUNNING" TASKLIST /M wbem* TASKLIST /S system /FO LIST TASKLIST /S system /U domain\username /FO CSV /NH TASKLIST /S system /U username /P password /FO TABLE /NH TASKLIST /FI "USERNAME ne NT AUTHORITY\SYSTEM" /FI "STATUS eq running"
3. for loop
1. Read text content @echo off for /f %%i in (test.txt)do ( echo %%i ) pause > nul 2. Tokens of /f The contents of the file are as follows: root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin @echo off for /f "delims=: tokens=7" %%i in (test.txt)do ( echo %%i ) The result is as follows: /bin/bash /sbin/nologin
2. Specific script
1. Background execution program script bat
@echo off powershell.exe -command " & amp; {Start-Process -WindowStyle hidden -FilePath 'D:\develop\frp\frpc.exe' '-c D:\develop\frp\frpc.ini' -RedirectStandardOutput 'D:\ develop\frp\frp_log.log'}" powershell.exe -command " & amp;{ tasklist /fi 'IMAGENAME eq frpc.exe' > D:\develop\frp\ ow_pid.pid}"
Execution script explanation
①I am using the exe execution program of frpc. I need to pass a parameter -c plus configuration file, so it is the above reference method;
②I output the program returned by the command to the file specified in -RedirectStandardOutput parameter. For details, please see the link introduction of the Start-Process command;
③The execution content of cmd is:
D:\develop\frp\frpc.exe -c D:\develop\frp\frpc.ini
④Output the pid called by execution to the corresponding file
Output image
2. Check whether the execution is executed and terminate the program
@echo off rem turns on delayed expansion setlocal enabledelayeexpansion rem gets the pid of the program named example.exe for /f "tokens=2 delims=," %%a in ('tasklist /nh /fo csv /fi "imagename eq frpc.exe"') do ( rem removes double quotes set pid=%%~a rem print pid echo The pid of example.exe is [ !pid! ] ) pause taskkill /pid !pid!
Check stop script explanation
①rem Enable delayed expansion setlocal enabledelayeexpansion
②delims=xxx – refers to the delimiter set. This replaces the default set of delimiters for spaces and tabs.
tokens=x – refers to which token of each line is passed to each iteration
③pause If you press any key to continue, the stop operation will be performed. It can also be removed.
④taskkill command stop command
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM imagename] } [/T] [/F] describe: Use this tool to terminate tasks by process ID (PID) or image name. parameter list: /S system specifies the remote system to connect to. /U [domain\]user Specifies the user context in which this command should be executed. /P [password] Specifies the password for the provided user context. If ignored, prompt enter. /FI filter Applies a filter to select a set of tasks. with permission "*". For example, image name eq acme* /PID processid Specifies the PID of the process to terminate. Use TaskList to get PID. /IM imagename Specifies the image name of the process to terminate. The wildcard character '*' can be used Specify all task or image names. /T Terminates the specified process and child processes started by it. /F specifies the forced termination of the process. /? Display help message. Filter: Filter name Valid operators Valid values ---------- --------------- ---------------------------- - STATUS eq, ne RUNNING | NOT RESPONDING | UNKNOWN IMAGENAME eq, ne image name PID eq, ne, gt, lt, ge, le PID value SESSION eq, ne, gt, lt, ge, le Session number. CPUTIME eq, ne, gt, lt, ge, le CPU time, the format is hh:mm:ss. hh - when, mm - minutes, ss - seconds MEMUSAGE eq, ne, gt, lt, ge, le memory usage in KB USERNAME eq, ne username in the format [domain\]user MODULES eq, ne DLL name SERVICES eq, ne service name WINDOWTITLE eq, ne window title illustrate ---- 1) The /IM switch can use the wildcard character '*' only if a filter is applied. 2) Remote processes are always terminated forcefully (/F). 3) "WINDOWTITLE" and "STATUS" filters are not supported when specifying a remote machine. For example: TASKKILL /IM notepad.exe TASKKILL /PID 1230 /PID 1241 /PID 1253 /T TASKKILL /F /IM cmd.exe /T TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*" TASKKILL /F /FI "USERNAME eq NT AUTHORITY\SYSTEM" /IM notepad.exe TASKKILL /S system /U domain\username /FI "username ne NT*" /IM * TASKKILL /S system /U username /P password /FI "IMAGENAME eq note*"