简体   繁体   中英

system command line max length in perl

To reduce the number of commands issued, I need to put as many characters as I can in the perl statement

system("$cmd_and_parameters")

What is the (safe) max line length I can assume, both under Windows and Linux?

My question is due to the need to issue commands of the Oetiker "rrdtool suite", without using the proper perl modules (please, don't blame me for this).

My first attempt was using the before mentioned function, setting

$cmd_and_parameters = "path_to_rrdtool/rrdtool update $db $timestamp:$value" 

It works, but it's slow, considering the number of updates I have to do.

So, as "rrdtool update" allows it, I decided to increase the number of arguments up to the maximum allowed, reducing the number of "system" calls.

Using a reasonable small number of arguments, it works much better, and anyway it's good enough to my needs, but still remains the question "How many $timestamp:$value couples can I put in the system function?

That is to say: how many characters can be in a perl system function?

On Linux, the limit is ARG_MAX (which you could query with getconf ARG_MAX ). I have th habit of editing my kernel's limit (eg in linux-3.7.1/include/uapi/linux/limits.h to increase it).

(Notice that setrlimit(2) RLIMIT_STACK may in practice lower the maximal size of program arguments)

Some Linux kernels (notably old, or embedded) have a lower limit. You should be prepared to have it as low as perhaps 32768 (on my own kernel I'm raising it to 2097152).

The reason I am raising my ARG_MAX is that I have plenty of RAM (16Gbytes), my shell is zsh and I love typing **/*.c when I want to (thus avoiding a find )

Read the execve(2) Linux man page and the Posix exec documentation. The Linux man page has several paragraphs about Limits on size of arguments and environment .

On Windows (or at least on MS-DOS) it might be as low as 128 (before expansion).

And you should understand that argument expansion is (on Posix & Linux system) done by the shell, and that system(3) and Perl's function of that name probably is starting a /bin/sh -c which will do the expansion. What matters is the size of the expanded argument list: so if you call system("ls */*/*.c") and if */*/*.c expands to a million arguments you are in trouble (at least on Posix and Linux) even if the unexpanded command line ls */*/*.c is very short.

On Windows and MS-DOS the runtime library is rumored to expand the command line into an argument list. I heard that the program starts with the terminal command uninterpreted, and some runtime startup thing is doing the expansion.

So to answer your question: on Posix systems, what matters is the expanded argument list, and if that expanded argument list is smaller than 32K bytes you are very probably very safe (In practice 128Kbyte is good enough). On Windows or MS-DOS (I am guessing that) you probably need to limit the unexpanded command line to 128 chars and its expansion to 64Kbytes (in practice bigger thresholds would often work). I really think you should always test the result of system

Basile has pretty much conclusively answered the Linux answer.

Windows NT and related versions (That means NOT Windows 95, 98 or ME - they are, I think limited to 128 charcters including command name) has a command line of either 2047 or 8191 characters. http://support.microsoft.com/kb/830473

Unless you really need to support ancient (12+ year old) versions of Windows, 2KB minus a tiny bit should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM