Search code examples
batch-filetasklist

Is there a way for tasklist on windows to read variables made by set?


I'm trying create a which checks if my mysql server is up and running before running other stuff. I'm trying to do this by reading the .pid file, (set to a variable, %pid%), then ask check for it.

The problem is that tasklist doesn't seem to like the % sign, so it's a bit hard to do this. Every time I try do to this I get:

ERROR: The search filter cannot be recognized.

I don't think you can read variables any other way but I'm relatively new to batch scripting so I may be wrong.

Heres a copy of the script:

set pid=C:\mysql\data\DESKTOP-MG3L38L.pid
rem making sure the pid var exists
type %pid%
tasklist /fi 'pid eq %pid%

Solution

  • Your real issue is that you're not Setting your variable to the content of the first line of your file; you're Setting it to the name of the file.

    To set it to the content of the first line of the file you would use:

    @Set /P "PID="<"C:\mysql\data\DESKTOP-MG3L38L.pid"
    

    Because you're not setting it correctly TaskList is trying to filter the name of the file, C:\mysql\data\DESKTOP-MG3L38L.pid as the numeric integer sequence, which will obviously fail.

    Once, you've fixed that, you can run your TaskList command:

    @TaskList /Fi "PID Eq %PID%" /Fi "Status Eq Running"