• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 941
  • Last Modified:

how to end & restart process via a batch file.

I want a simple batch file that will kill a process and then restart that same process.
right now i'm doing it via 'services' window but i want it via a batch file.
i'm testing this with notepad.exe before moving forward with an actual service. not sure if coding would be diff with a service versus an application.

but for my notepad test i have

TASKKILL /F /IM "notepad.exe"

this kills notepad, but then how do i reopen a new notepad? and would these steps be same for a service?
0
badtz7229
Asked:
badtz7229
3 Solutions
 
hunartCommented:
Taskkill will kill process by process ID.  It's useful when you wanted to kill a hung application etc...

If you would like to kill a Windows Service, use the Net Start/Net Stop/Net Restart/Net Resume etc...

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
To reopen notepad you would just add a new line:

notepad.exe <filename>  

<Filename> part could be passed in as a parameter to load a file, but is not required.

For services Hunart is correct, though to add the usual format that I have found to work is

net stop "svc name"
net start "svc name"



0
 
bminkerCommented:
Services

net stop "print spooler"
net start "print spooler"

Notepad

TASKKILL /F /IM "notepad.exe"
"notepad"

if you wanted a different program change the notepad to something similar to

"C:\Program Files\Microsoft Office\Office12\WINWORD.EXE"

make sure you have the quotes

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
badtz7229Author Commented:
bminker:
your example helped a lot. however, sometimes my process just hangs when i do it via the services window. so i have to forcefully kill it via task manager, i'm trying to incoporate using a taskkill and net start but although i'm able to successfully kill the process, i can't restart it back up

TASKKILL /F /IM "temp.exe"
Net Start "TEMP Server"

if i do this it works:
Net Stop  "TEMP Server"
Net Start "TEMP Server"

but i want to forcefully kill this service & then start it back up.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
The question becomes why does your service hang?  All services should start and end gracefully.  Would recommend you write some debugging code to the sys log to find out what's going on.
0
 
badtz7229Author Commented:
ged325:
sometimes it just states that the program didn't end in a timely manner and then just takes extra longer to close . that's why i wanted to just forcefully close it.
0
 
AmazingTechCommented:
Your code needs to stop the service before it can start it.

Do a Net stop and then kill the task if it's still running then start it.

Net Stop  "TEMP Server"
TASKKILL /F /IM "temp.exe"
Net Start "TEMP Server
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now