473,804 Members | 3,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How a script can know if it has been called with the -i command line option?

The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
TIA,

Michele Simionato

Dec 21 '06
11 1443

Peter Wang wrote:
Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?

I realize this is quite a hack, but the entire command line is
preserved in the process's entry in the OS's process table. if you do
"ps -ax" you will see that the interpreter was invoked with -i. I
didn't test this under windows, but it works on Mac and Linux.
That hack might not work - at least, as described, and on Linux or Mac
OS if the UNIX-based one, i.e. OS X). Because there could be other
users who ran python command lines with or without the -i option. As
described, there's no way for this user to know which python invocation
is his/hers, and which are of other users. There might be a way,
though, if we can get this user's python instance's process id and then
grep for a line containing that id (in the appropriate column) in the
ps output.

Vasudev Ram
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
Dancing Bison Enterprises
http://www.dancingbison.com
http://dancingbison.blogspot.com
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
Check out the cool Snap.com preview feature on my web site.
Free signup for anyone at www.snap.com
I'm not affiliated with it.

Dec 23 '06 #11

vasudevram wrote:
Peter Wang wrote:
Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
I realize this is quite a hack, but the entire command line is
preserved in the process's entry in the OS's process table. if you do
"ps -ax" you will see that the interpreter was invoked with -i. I
didn't test this under windows, but it works on Mac and Linux.

That hack might not work - at least, as described, and on Linux or Mac
OS if the UNIX-based one, i.e. OS X). Because there could be other
users who ran python command lines with or without the -i option. As
described, there's no way for this user to know which python invocation
is his/hers, and which are of other users. There might be a way,
though, if we can get this user's python instance's process id and then
grep for a line containing that id (in the appropriate column) in the
ps output.

Vasudev Ram
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
Dancing Bison Enterprises
http://www.dancingbison.com
http://dancingbison.blogspot.com
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
Check out the cool Snap.com preview feature on my web site.
Free signup for anyone at www.snap.com
I'm not affiliated with it.
Just realized: getting the python process's process id is possible from
the Python program itself, using os.getpid().

Vasudev

Dec 23 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
8297
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now with form elements that turn out to be arrays that have to be compared with one another! I have one form element, languages, a checkbox group. Beside each checkbox is a dropdown, proficiency (which will become proficiency alongside languages)....
0
4670
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { // CONSTRUCTOR $this->fileNameArray = array(); $this->isRemoved = 0; }
5
9206
by: deko | last post by:
In regard to running php scripts with cron - Here is a sample script: <?php //debug.php echo "<br> This is a test"; ?> I can call debug.php from a web page on my site like this:
2
22715
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql format. I'm trying to figure out how to import that into SQL Server 2000 so that I'll be able to manipulate it in a gui format, rather than command line. I can't find any import that takes a .sql file. I've been trying to load it into the query...
9
13762
by: _link98 | last post by:
Normally I use the semicolon for statement delimiters in plain SQL scripts (for DDL, simple DML etc.). But inside SQL/PL I tend to use % or @ as statement delimiters. But other people prefer other delimiters. I know that I can use the CLP option "-td" to specify the delimiter on the command line, when invoking the script. But this requires me to look inside the scripts to find the delimiter before I run the script.
13
6016
by: wattersmt | last post by:
Hello, I am trying to write a python cgi that calls a script over ssh, the problem is the script takes a very long time to execute so Apache makes the CGI time out and I never see any output. The script is set to print a progress report to stdout every 3 seconds but I never see any output until the child process is killed. Here's what I have in my python script:
2
1580
by: rowan | last post by:
I'm writing a Python script that can either be called as a Cron job or as a web page (i.e. as a CGI in response to an HTTP request). This is to process the mailboxes on my web server (to which I don't have command line access) to remove old messages. How do I find out whether the script has been called as a Cron job or as a CGI? I need to know this so I can format the output correctly, e.g. if this is a web request I need to start the...
51
4168
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under Command Prompt or Browser or some other application? Ojas.
1
47493
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.