discussing some technical aspects covering tools, frameworks, technologies etc (the areas where i m involved and getting experienced) - feel free to discuss!

Showing posts with label OS. Show all posts
Showing posts with label OS. Show all posts

Monday, September 08, 2008

Editing the shell script in Windows OS

Are you one among them who happened to edit the shell script in Windows OS? You may have various reasons to do that.

You should have got the script working very well in the appropriate linux (or any other flavour) box. But just before doing the final operation (be it checking it into CVS (version control system) or sending to the client waiting at onsite) you might have thought of giving a final/finishing touch like adding some documentation etc.,

Are you asking, "wats up?" or "what's a big deal.. huh?". Wait a minute. All is fine until you execute the script back in the linux box again.

Boooom.. The linux OS spits out an error message saying that "/bin/bash^M: bad interpreter: no such file or directory". What had happened?
Wondering..? Surprised? Shocked rather??

Well, here it goes. The way the Windows OS deals with the file saving option is quite different than that of Linux OS. Once you are done with the so called final/finishing touch and when you save the file, some control characters are getting added to the shellscript (.sh file). That's what the culprit is and hence the error when executing it again in linux. The Bash Shell is unable to deal with such special character ('Control M' -- is referred as "^M").

It is assumed that Bash is the default and widely used Shell. The shell being used and the actual error message might vary as per your system's default shell and the way it is customized.

What and Where is the rescue?

There has to be a way. Isn't it? Yes, of course. There are two options.

(1) You can open the file in Linux OS (using your favorite editor like
vi, emacs etc.,) and can delete such characters what you find at the end. I am not very sure of this as to what extent those characters are explicitly visible to the end user.

(2) But the other excellent alternative is to use the 'dos2unix' command wherein the usage goes as follows.

'dos2unix ' [eg., dos2unix myShellScript.sh]

This do2unix command converts the file in the DOS (for Microsoft environment) to the Unix properitary format. While doing so, it removes the special/control characters and it ultimately cleans the file to be used in Linux versions :).

Note/Word of Caution: Never edit the Linux related files in non-linux OS (Windows)! :)

Hope this helps to solve the tactics and saves you from the critical moment!

Sunday, March 09, 2008

dealing with windows services in java

&&tWell, had been thinking to get the control of windows services in a Java application so that programatically I can manage them (start/stop etc).

My main intention to start off this was basically to check the status of the service whether it is up and running. If not, start it before you accomplish the relevant task. The triggering factor was to check the 'MySQL' service as i sometimes keep certain services as of type 'Manual' instead of 'Automatic' so that these services may not take up considerable amount of time when the Windows OS gets booted up.

At times, when running a J2EE web application used to get the SQLException saying that 'could not obtain a connection', used to panic all of a sudden. Then recollecting our duty as having forgotten to start the particular service :)


As such, i am able to get the service up and bring it down through the application. Yet to get to know the status of a particular service and conditionally run the service :)

First, i remember i can get the external process running inside a Java appliation is via Runtime class (java.lang.Runtime) and its 'exec()' method which takes a String argument which says the OS process to run. A couple of years back i have invoked the IE browser via the same and recollected those stuff.

I tried executing the same as

  Runtime runtime = new Runtime().exec("iexplore.exe www.javaranch.com");

The second argument is the actual argument to the application/process to be executed.

 It was giving a different error saying that "java.io.IOException: CreateProcess iexplore.exe www.javaranch.com error=2". Had searched a bit in JavaDoc of the Java API in Process, Runtime classes, but really could not get anything. 

 Beforehand, i wanted to try with some other executables like Notepad etc., to my surprise it was working fine. for example the below command

  runtime.exec("Notepad.exe C:\path.txt");

  perfectly works fine and opens the path.txt inside the notepad application.  Have checked the same with "Calc.exe" as well.

 I have concluded to certain extent that the way we invoke this particular executable is errenous. I have tested the same by invoking the executable in the command prompt. It gave the infamous error

'iexplore.exe' is not recognized as an internal or external command,

   Found out that the path was missing to this executable. That means, the location to the 'iexplore.exe' was not added into the path.  Though i checked the same by adding the location to IE.exe in PATH and it worked fine, but it won't be appropriate to do so just for this testing purpose. So reverted back and tried executing the following:

  runtime.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe www.javaranch.com");

  What a surprise? It worked fine! 

 Then i moved on to my required, 'services.msc' stuff in the same way.  I got the same error but with different error code even when i gave it with full path :(. For the invocation of 

   runtime.exec("C:\\WINDOWS\\system32\\services.msc");

  the error what i got was, "java.io.IOException: CreateProcess: C:\WINDOWS\system32\services.msc error=193". I am sure that the path to 'System32' was not mandatory at all. This error made me get into google and some good links helped me out in finding a way of what this error 193 means, 'this is not a valid Win32 application'.  Also could get some other invocations and out of which what i tried was successful. It is below.

  runtime.exec("cmd.exe /c services.msc");

  Actually, you have to invoke the Command Prompt (cmd.exe) first and then pass the actual service/exectuable to be invoked as an argument to command prompt. The middle argument '/c' is for 'carrying out the command and terminates". And i thought it would work even if it is not present, but it failed :(. Need a clarification here. :).  But it holds good when you type it in 'Run prompt'.  The other option to verify it is '/K' which stands for 'carries out the command specified by the string but remains'. 

  The next question was, how to get the particular service up? I tried by passing the name of the service as an argument as follows.

  runtime.exec("cmd.exe /c services.msc MySQL");

  but it dint give any fruitful results :(.  After a while, though why don't i directly execute the particular executable which the service is made run. Of course, the services is also a shortcut or link pointing to the executable placed elsewhere. Tried it out. it worked but with two drawbacks..

   1. The java executable was getting hung. :(
   2. Was not having a way to stop the executable rather than killing the process!

  Finally when continued searching in google, somewhere i read about the way to start a service. That's through 'NET START '. Here is the link. Got succeeded as well :)
   
  runtime.exec("net start MySQL");
  runtime.exec("net stop MySQL");
 
  Obviously these are invoking the MySQL Service in the services.msc only. As of now i am able enuf to do till this phase. Let me complete the rest and post it here.. 
  

to set the color of the windows cmd prompt


Basically I  too fall in that 'customizing-freak' category to customize certain features of the OS apart from the specific applications we work on.

One among them is, to have the preferred font, foreground and background colors in the Comman Prompt and with a shortcut to the command prompt on the Quick Launch bar.

Just today when i was searching for a help command for another utility (services.msc), got to know about this favorite-and-regular routine of mine. 

Windows has a command utility named Color with an argument to set the foreground and background colors for the window being opened.

I bet no one can beat the Microsoft's help/documentation. so for  your info, just copy-pasting the information available in the help!

Command : color help

Sets the default console foreground and background colors.

COLOR [attr]

  attr        Specifies color attribute of console output

Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground.  Each digit
can be any of the following values:

    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White

If no argument is given, this command restores the color to what it was
when CMD.EXE started.  This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.

The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the
same.

Example: "COLOR fc" produces light red on bright white.

My preference goes as : "Color 0a" :P

Note: It just sets the colour for the current window being opened! :)

Another Info about the 'DefaultColor' registry value: it is present in the HKEY_USERS/.DEFAULT/Software/Microsoft/CommandProcessor directory in 'regedit' (Registry Editor).

Monday, November 19, 2007

tasklist - cmd in windows similar to ps cmd in linux

The people who have worked in Unix and its flavours, would have definitely come across a command called 'ps' (which stands for 'process status'). As such the 'ps' command gives you the list of processes running and the pid (process id) etc.,

There is a command to achieve the same in Windows OS too. That is 'tasklist'. Just execute in the command prompt. Here it gives the "Image Name", "PID", "Session name", "Session#", "Mem Usage".

Image Name -> the name of the process running
PID -> as you know, the process id (unique id which the OS refers to)
Session name -> it shows 'Console' but have to check what it really means
Session # -> should be a session id and for console it gives '0'.
Mem usage -> the amount of memory each task/process occupies.


Here is the sample screenshot of the same.


It looks like the CUI version of the 'task manager' :)

NOTE: It may not work in all versions of Windows. As such i have tested in Windows XP. I believe it must definitely work in 2000 also. Not sure about the previous versions of 98,95 etc., will check and update later.

About Me

ஏதோ பிறந்தோம், ஏதோ வாழ்ந்தோம் என்றிருப்பதல்ல வாழ்க்கை! எப்படி வாழ்ந்தோம் என்பதும் ஒரு அங்கம். வாழக் கிடைத்த வாழ்க்கையில், ஒரு சிலருக்காவது வசந்தத்தின் முகவரியை அறிமுகம் செய்தோமேயானால் அதுவே வசீகரத்தின் வனப்பைக் கூட்டும்!