How to Close Firefox (or any program) at Command Line/Prompt

Discuss various technical topics not related to Mozilla.
Locked
frankvii
Posts: 3
Joined: May 21st, 2015, 8:12 am

How to Close Firefox (or any program) at Command Line/Prompt

Post by frankvii »

Firefox for Windows doesn't have a command line option to close it. However, I've found two methods that can be used to close it (or any other program) from a command line/prompt. The first method requires using a free, small (43 kb), DOS enhancer program called NIRCMD. The second uses TaskKill which is a native Windows 7 process.

For NIRCMD: You need to download the file from http://nirsoft.net/utils/nircmd.html . Put nircmd.exe in a convenient location, like C:\Windows (Note: nircmd.exe, uses a message box for errors while nircmdC.exe doesn't, that's the only difference). Run firefox and in "Options" insure the warnings for closing tabs are NOT selected (it will prevent closing). Now, a command line of " nircmd.exe closeprocess firefox.exe " will gracefully close Firefox.

For TaskKill: All you have to do is, at the command line, enter " TASKKILL /F /IM firefox.exe /T ". This had the advantage of working without an additional piece of software and not requiring a change in Firefox tab options. A disadvantage MAY (a guess on my part) is that it could corrupt a firefox file that is being updated at the moment (e.g. a cache file). I've tried to research this possibility but didn't turn up anything.

As mentioned, since NIRCMD and TASKKILL are generic programs, they can be used on any other running program. NIRCMD has an additional advantage in that it is a Swiss Army knife of programs. It does almost everything that you had always wished DOS could do. It includes a CHM file that contains examples and seems to include all possible uses of NIRCMD.

If you are reading this, then you have your specific reasons for wanting to close firefox from a command line. I am using it as part of a backup script and firefox must be closed to perform a full backup.
User avatar
DanRaisch
Moderator
Posts: 127187
Joined: September 23rd, 2004, 8:57 pm
Location: Somewhere on the right coast

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by DanRaisch »

Moving to MozillaZine Tech as this is not really a Firefox Support issue.
User avatar
trolly
Moderator
Posts: 39851
Joined: August 22nd, 2005, 7:25 am

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by trolly »

To clarify the usage of taskkill:
taskkill /F instructs the OS to immediately stop the process and remove it from memory. All currently active operations are aborted (program buffers are not flushed which can lead to incomplete files, system locks may not be released and such things).
The SQLite databases in firefox automatically recover from such states on next start, unless in very severe failures. But the last change may be lost.
If you want a graceful shutdown do not use the /F (force) flag but the process may not obey (e.g. firefox may ask the user if it should close all windows and tabs).

The technical background:
The normal, graceful shutdown simply sends a "window close" command to the main window. This is the same as clicking on the X button in the top right corner.
Think for yourself. Otherwise you have to believe what other people tell you.
A society based on individualism is an oxymoron. || Freedom is at first the freedom to starve.
Constitution says: One man, one vote. Supreme court says: One dollar, one vote.
User avatar
malliz
Folder@Home
Posts: 43796
Joined: December 7th, 2002, 4:34 am
Location: Australia

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by malliz »

Stupidist thing I have read in a long time good way to corrupt any number of programs and files. Do not follow this advice
You have been warned
What sort of man would put a known criminal in charge of a major branch of government? Apart from, say, the average voter.
"Terry Pratchett"
User avatar
patrickjdempsey
Posts: 23686
Joined: October 23rd, 2008, 11:43 am
Location: Asheville NC
Contact:

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by patrickjdempsey »

Especially ironic given that it's being used in conjunction with a backup script. Doing this will ensure that your backups will mostly contain corrupted files.
Tip of the day: If it has "toolbar" in the name, it's crap.
What my avatar is about: https://addons.mozilla.org/en-US/seamonkey/addon/sea-fox/
User avatar
malliz
Folder@Home
Posts: 43796
Joined: December 7th, 2002, 4:34 am
Location: Australia

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by malliz »

The only way you could do potentially more damage is by pulling the power cord
What sort of man would put a known criminal in charge of a major branch of government? Apart from, say, the average voter.
"Terry Pratchett"
frankvii
Posts: 3
Joined: May 21st, 2015, 8:12 am

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by frankvii »

patrickjdempsey wrote:Especially ironic given that it's being used in conjunction with a backup script. Doing this will ensure that your backups will mostly contain corrupted files.


Just to clarify, I am using NIRCMD to close firefox, not Taskkill. In researching how to close Firefox, the only solution I could find on the internet was the Taskkill option which, as I stated, seemed like it could possibly corrupt firefox. In the past I have used NIRCMD for a number of other things and it occurred to me that perhaps it could also close firefox, which it can, and it does so in a normal manner. So I posted what I found here.

Obviously the consensus, including the moderators, is not to use Taskkill. With a free NIRCMD, there is really no reason to.
User avatar
malliz
Folder@Home
Posts: 43796
Joined: December 7th, 2002, 4:34 am
Location: Australia

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by malliz »

You obviously have no idea what tasks Firefox goes through when shutdown correctly and yet you still decided to give inherently bad advice on a public forum to promote a free program please don't do that.
What sort of man would put a known criminal in charge of a major branch of government? Apart from, say, the average voter.
"Terry Pratchett"
morat
Posts: 6404
Joined: February 3rd, 2009, 6:29 pm

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by morat »

@frankvii

A good solution. I tried the following command and I got a "Confirm close" dialog as expected.

Code: Select all

nircmdc.exe closeprocess firefox.exe

http://nirsoft.net/utils/nircmd2.html#closeprocess
Last edited by morat on May 22nd, 2015, 6:26 am, edited 1 time in total.
frankvii
Posts: 3
Joined: May 21st, 2015, 8:12 am

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by frankvii »

malliz wrote:You obviously have no idea what tasks Firefox goes through when shutdown correctly and yet you still decided to give inherently bad advice on a public forum to promote a free program please don't do that.


Look, what you call "advise" and promoting a program is just me trying to share the only two options I could find that would shutdown firefox from the command-line. This topic was originally posted in the Firefox Support/Help section but the moderator moved it here. However, the Tech section is an area where I would NEVER post. I am certainly no Tech (of any kind) and, you are right, I do NOT know the tasks Firefox goes through to shutdown. However, the "inherently bad advise" I gave included the following: "A disadvantage MAY (a guess on my part) is that it could corrupt a firefox file... I've tried to research this possibility but didn't turn up anything". I think this shows an admitted lack of knowledge, that I searched for this knowledge and gave an appropriate corruption warning (based upon my limited knowledge).

One final comment, I'd like to thank Trolly, the Moderator. The reply given shared what I couldn't find,that is, how Taskkill might corrupt firefox.
User avatar
trolly
Moderator
Posts: 39851
Joined: August 22nd, 2005, 7:25 am

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by trolly »

No need for name calling.
We all know now what was intended. And the problem is solved now.

Just as information what can be corrupted and its resolution:
- cache -> it is deleted and recreated.
- cookies -> deleted (usually), strange malfunctions may require manual removal
- databases -> reconstructed with possible loss of last change
- occasionally the profile lock file is not removed -> must be manually removed to be able to use the profile again

I guess that's the important parts.
Think for yourself. Otherwise you have to believe what other people tell you.
A society based on individualism is an oxymoron. || Freedom is at first the freedom to starve.
Constitution says: One man, one vote. Supreme court says: One dollar, one vote.
sludge7071-g
Posts: 100
Joined: May 9th, 2016, 10:18 am

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by sludge7071-g »

So the conclusion is:

The command line of "nircmd.exe closeprocess firefox.exe" is fine . . . Taskkill is bad
User avatar
DanRaisch
Moderator
Posts: 127187
Joined: September 23rd, 2004, 8:57 pm
Location: Somewhere on the right coast

Re: How to Close Firefox (or any program) at Command Line/Pr

Post by DanRaisch »

And that conclusion is from two years ago.

Locking this thread due to the age of the original posts.
Locked