问题
I want to kill web job so how can I get web job process id?
is it possible to get web job process id & kill a web jobs using programming?
回答1:
I want to kill web job so how can I get web job process id?is it possible to get web job process id & kill a web jobs using programming?
According to your description, I suggest you could use kudu webjob stop rest api stop the webjob.
The web job will atuomatic restart after you killed the process.
You could use Kudu rest api to achieve your requirement.
You could firstly set a Deployment credentials in your azure web application as below:
Notice:Remember the user name and password, we will use them to generate the access token
Then you could write the code to send the request to stop your web job as below:
Url:https://yourwebsitename.scm.azurewebsites.net/api/continuouswebjobs/TestWebJob/stop
More details, you could refer to below codes:
string url = @"https://yourwebsitename.scm.azurewebsites.net/api/continuouswebjobs/TestWebJob/stop";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = 0;
string logininforation = "usename:password";
byte[] byt = System.Text.Encoding.UTF8.GetBytes(logininforation);
string encode = Convert.ToBase64String(byt);
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);
// Get the response
HttpWebResponse httpResponse = null;
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Console.WriteLine(httpResponse.StatusCode);
If you still want to kill the process, I suggest you could send the request to below url to get the process firstly.
Url:https://yourwebsitename.scm.azurewebsites.net/api/processes
If you get the process the you could send a delete request to the kudu to kill the webjob's process.
More details, you could refer to below C# codes:
string url = @"https://yourwebsitename.scm.azurewebsites.net/api/processes";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";
httpWebRequest.ContentLength = 0;
string logininforation = "username:password";
byte[] byt = System.Text.Encoding.UTF8.GetBytes(logininforation);
string encode = Convert.ToBase64String(byt);
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (System.IO.StreamReader r = new System.IO.StreamReader(response.GetResponseStream()))
{
string jsonResponse = r.ReadToEnd();
dynamic result = JsonConvert.DeserializeObject(jsonResponse);
dynamic resultList = result.Children();
foreach (var item in resultList)
{
Console.WriteLine(item.name);
if (item.name == "yourwebjobname")
{
Console.WriteLine(item.href);
//begin to delete the webjob process
string url2 = item.href;
var httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url2);
httpWebRequest2.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);
httpWebRequest2.Method = "DELETE";
httpWebRequest2.ContentLength = 0;
HttpWebResponse httpResponse2 = null;
httpResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse();
}
}
}
}
来源:https://stackoverflow.com/questions/44110819/how-do-i-get-process-id-of-particular-webjobs-in-azure