Is there a way to set argv[0] in a Haskell program (say, one compiled with ghc)?
I found the getProgName and withProgName functions in System.Environment, but it doesn’t seem to change what ps reports (Ubuntu).
import System.Environment
main =
do name <- getProgName
putStrLn $ "Hello, my name is " ++ name
withProgName "other" $ do
newname <- getProgName
putStrLn $ "Name now set to " ++ newname
putStrLn "What is your name: "
-- allow time to run ps
ans <- getLine
putStrLn $ "Pleased to meet you, " ++ ans
There is no portable way of doing this, but on Linux 2.6.9 and up the process name can be changed with
prctl()using thePR_SET_NAMEoperation, so we just need a little bit of FFI to use it from Haskell. (It’s usually a good idea to check if there are any bindings on Hackage, but in this case I couldn’t find any).This seems to work fine for changing the name as seen by
ps. However, the value returned bygetProgNameappears to be cached when the program starts, so you’ll have to combine this withwithProgNameto see the change within your program.