1

I created a batch file as below:

set VAR1=VALUE1
start another_batch_file
sleep 10
ps -l |grep abc > foo.txt

another_batch_file would start several processes, I want to use ps to list some specific ones. This script works well when I run it from a cmd window, although if I create a task in Task Scheduler to call this script automatically the foo.txt will be empty.

I also tried to output the ps -l result to a file, it's also empty

ps & grep both from MKS toolkit.

Edit:

Per the comments, I have tried with the script similar to the following:

set VAR1=VALUE1
start another_batch_file
sleep 10
ps > foo.txt
tasklist > bar.txt

Now foo.txt is still empty, while bar.txt has normal output which contains all the processes' information.

Any one has a clue?

Edit 2:

I have the box "Run with highest privileges" checked and the the ps command works well now.

I have been using the same user account when run the script in a cmd window or by a scheduled task, although the ps exeutable file located in a mapped drive, I think this caused the script needs higher privileges to run

poiu2000
  • 960
  • 2
  • 14
  • 30
  • I suppose that `grep abc` searches for `another_batch_file`? – jeb Oct 21 '13 at 07:05
  • yes, in fact cmd is one of my grep target, I just don't know why the foo.txt file is empty in Task Scheduler case – poiu2000 Oct 21 '13 at 07:11
  • If it's empty, then remove the `grep` part, to see what the output is and how you should create the best grep expression for it – jeb Oct 21 '13 at 07:16
  • I have tried to output the result of `ps -l`, it's also empty, I have edit the questioni about this, thank for reminding this – poiu2000 Oct 21 '13 at 07:22
  • Do you get any output from `ps`? Can you redirect it to a file from the command line? – jeb Oct 21 '13 at 07:28
  • I have tried and it's also empty – poiu2000 Oct 21 '13 at 07:33
  • And what is the output of `ps` without redirection? – jeb Oct 21 '13 at 07:44
  • I don't know how to get the output without redirect to a file when run this script from Task Scheduler, I run this script from a cmd window and out put of ps is normal – poiu2000 Oct 21 '13 at 08:01

2 Answers2

3

If it works at the prompt: task scheduler runs in the system account by default and has no access to network resources, and if your other batch file is trying to access them then it will not load.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
2

A batch file isn't never in the process list. Batch files are executed by cmd.exe, so cmd.exe is in the list.

But you should be able to access it with ps, if you add a switch for displaying the parameters, too.

It's also possible with tasklist.

tasklist /FI "IMAGENAME eq cmd.exe" /V | grep another_batch_file

C:\Windows\system32\cmd.exe - another_batch_file.bat cmd.exe
1320 Console 1 3.132 K Unknown

jeb
  • 78,592
  • 17
  • 171
  • 225