Re: Find the path of a shell command

2022-10-17 Thread Grant Edwards
On 2022-10-15, Andreas Eder  wrote:
> On Mi 12 Okt 2022 at 05:00, Paulo da Silva 
>  wrote:
>
>> The simple question: How do I find the full path of a shell command
>> (linux), i.e. how do I obtain the corresponding of, for example,
>> "type rm" in command line?
>>
>> The reason:
>> I have python program that launches a detached rm. It works pretty
>> well until it is invoked by cron! I suspect that for cron we need
>> to specify the full path.

> Why not just use os.unlink ?

Because he's using 'rm' to recursively remove a large directory tree
many levels deep.

One might think that could be done by 

   os.removedirs(name)
   Remove directories recursively. [...]

But it doesn't appear so from the description:

Works like rmdir() except that, if the leaf directory is
successfully removed, removedirs() tries to successively remove
every parent directory mentioned in path until an error is raised
(which is ignored, because it generally means that a parent
directory is not empty). For example, os.removedirs('foo/bar/baz')
will first remove the directory 'foo/bar/baz', and then remove
'foo/bar' and 'foo' if they are empty. Raises OSError if the leaf
directory could not be successfully removed.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-17 Thread Andreas Eder
On Mi 12 Okt 2022 at 05:00, Paulo da Silva 
 wrote:

> The simple question: How do I find the full path of a shell command
> (linux), i.e. how do I obtain the corresponding of, for example,
> "type rm" in command line?
>
> The reason:
> I have python program that launches a detached rm. It works pretty well
> until it is invoked by cron! I suspect that for cron we need to specify
> the full path.
> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
> What about other commands?

Why not just use os.unlink ?

'Andreas
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command [POSTPONED]

2022-10-16 Thread Peter J. Holzer
On 2022-10-16 17:21:03 +0100, Barry wrote:
> > On 16 Oct 2022, at 04:53, Dan Stromberg  wrote:
> > 
> > On Wed, Oct 12, 2022 at 9:57 PM Cameron Simpson  wrote:
> > 
> >>> On 13Oct2022 03:25, Paulo da Silva 
> >>> wrote:
> >>> There is another problem involved. The script, works fine except when
> >>> launched by cron! Why?
[... script deleted ...]
> >> Cron's environment is very minimal. This will show you what's in
> >> it.
> > 
> > Careful.  On some systems if someone restarts the cron daemon, it could
> > pick up a larger environment than after being started on boot.
> 
> That have to a old system that does not use systemd.

Systemd is specific to Linux - and not even used by all Linux
distributions.

> Is there a specific system that still does this?

Not sure. Some Unixes I've used in the past (probably including some
Linux distributions) had this problem. So you had to be a bit careful
when restarting daemons from the command line. I think the sysVinit
system used by most Linux distributions before systemd did clean up the
environment if you used it correctly (but I'm not sure and have no
system anymore to test it). I haven't used Solaris or HP/UX in a long
time (and other Unixes even longer) so I don't know what they do these
days. And I've never used MacOS, I just know that they've used a system
of their own long before systemd.

Anyway he doesn't have to restart cron to add a cron-job (nor for any
other reason during normal operation), so what happens if you manually
restart cron is almost certainly irrelevant for the OP.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command [POSTPONED]

2022-10-16 Thread Barry


> On 16 Oct 2022, at 04:53, Dan Stromberg  wrote:
> 
> On Wed, Oct 12, 2022 at 9:57 PM Cameron Simpson  wrote:
> 
>>> On 13Oct2022 03:25, Paulo da Silva 
>>> wrote:
>>> There is another problem involved. The script, works fine except when
>>> launched by cron! Why?
>> 
>> Record the script output:
>> 
>> # record all output
>> exec >/tmp/script.$$.out 2>&1
>> # dump the envionment
>> env | sort
>> # turn on execution tracing
>> set -x
>> ... rest of the script
>> 
>> and have a look afterwards. Cron's environment is very minimal. This
>> will show you what's in it.
>> 
> 
> Careful.  On some systems if someone restarts the cron daemon, it could
> pick up a larger environment than after being started on boot.

That have to a old system that does not use systemd.
Is there a specific system that still does this?

Barry

> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command [POSTPONED]

2022-10-15 Thread Dan Stromberg
On Wed, Oct 12, 2022 at 9:57 PM Cameron Simpson  wrote:

> On 13Oct2022 03:25, Paulo da Silva 
> wrote:
> >There is another problem involved. The script, works fine except when
> >launched by cron! Why?
>
> Record the script output:
>
>  # record all output
>  exec >/tmp/script.$$.out 2>&1
>  # dump the envionment
>  env | sort
>  # turn on execution tracing
>  set -x
>  ... rest of the script
>
> and have a look afterwards. Cron's environment is very minimal. This
> will show you what's in it.
>

Careful.  On some systems if someone restarts the cron daemon, it could
pick up a larger environment than after being started on boot.
-- 
https://mail.python.org/mailman/listinfo/python-list


systemd Re: Find the path of a shell command

2022-10-14 Thread Weatherby,Gerard
Only the first one or two require more work. The rest are your copy-pastes from 
the last time you did one.

Typically I only have to changed descriptions, the executable in the *service 
and the timing in the *timer. They’re lot more readable and flexible than the 
cronjobs.

From: Python-list  on 
behalf of Albert-Jan Roskam 
Date: Friday, October 14, 2022 at 1:59 PM
To: Peter J. Holzer 
Cc: python-list@python.org 



   =
   Lately I've been using systemd timers instead of cronjobs. They are easier
   to debug (journalctl) but require a bit more work to write. Systemd is
   available on Fedora & friends and Debian based systems, maybe more. It has
   no builtin MAILTO. I use an OnFailure stanza to send a Slack message with
   curl instead.
   
https://urldefense.com/v3/__https://www.freedesktop.org/software/systemd/man/systemd.timer.html__;!!Cn_UX_p3!l6ZRc2Ur6rwspaC1V71wsENqmq5RNIR3lU2suC1LJLzYp097e-r2NwNBD3a_RhjuUkrSmfq3emmUsY-1m0PxT2TM$
   
https://urldefense.com/v3/__https://unix.stackexchange.com/questions/278564/cron-vs-systemd-timers__;!!Cn_UX_p3!l6ZRc2Ur6rwspaC1V71wsENqmq5RNIR3lU2suC1LJLzYp097e-r2NwNBD3a_RhjuUkrSmfq3emmUsY-1m6sDQHR0$
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!l6ZRc2Ur6rwspaC1V71wsENqmq5RNIR3lU2suC1LJLzYp097e-r2NwNBD3a_RhjuUkrSmfq3emmUsY-1m3Q-bOmf$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-14 Thread Albert-Jan Roskam
   On Oct 14, 2022 18:19, "Peter J. Holzer"  wrote:

 On 2022-10-14 07:40:14 -0700, Dan Stromberg wrote:
 > Alternatively, you can "ps axfwwe" (on Linux) to see environment
 > variables, and check what the environment of cron (or similar) is.  It
 > is this environment (mostly) that cronjobs will inherit.

 The simplest (and IMHO also most reliable) way to find out the
 environment a cronjob has is to write a cronjob which just dumps the
 environment.

   

   =
   Lately I've been using systemd timers instead of cronjobs. They are easier
   to debug (journalctl) but require a bit more work to write. Systemd is
   available on Fedora & friends and Debian based systems, maybe more. It has
   no builtin MAILTO. I use an OnFailure stanza to send a Slack message with
   curl instead.
   https://www.freedesktop.org/software/systemd/man/systemd.timer.html
   https://unix.stackexchange.com/questions/278564/cron-vs-systemd-timers
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-14 Thread Peter J. Holzer
On 2022-10-14 07:40:14 -0700, Dan Stromberg wrote:
> Alternatively, you can "ps axfwwe" (on Linux) to see environment
> variables, and check what the environment of cron (or similar) is.  It
> is this environment (mostly) that cronjobs will inherit.

The simplest (and IMHO also most reliable) way to find out the
environment a cronjob has is to write a cronjob which just dumps the
environment.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-14 Thread Dan Stromberg
On Wed, Oct 12, 2022 at 11:13 AM Paulo da Silva <
p_d_a_s_i_l_v_a...@nonetnoaddress.pt> wrote:

> Hi!
>
> The simple question: How do I find the full path of a shell command
> (linux), i.e. how do I obtain the corresponding of, for example,
> "type rm" in command line?
>
> The reason:
> I have python program that launches a detached rm. It works pretty well
> until it is invoked by cron! I suspect that for cron we need to specify
> the full path.
> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
> What about other commands?
>
I like to test my cronjobs with something like:

env - /usr/local/bin/my-cronjob

This will empty out the environment, and force you to set $PATH yourself.

Alternatively, you can "ps axfwwe" (on Linux) to see environment variables,
and check what the environment of cron (or similar) is.  It is this
environment (mostly) that cronjobs will inherit.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-13 Thread Michael Torrie
On 10/11/22 22:00, Paulo da Silva wrote:
> Hi!
> 
> The simple question: How do I find the full path of a shell command 
> (linux), i.e. how do I obtain the corresponding of, for example,
> "type rm" in command line?
> 
> The reason:
> I have python program that launches a detached rm. It works pretty well 
> until it is invoked by cron! I suspect that for cron we need to specify 
> the full path.
> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
> What about other commands?

There are certain standards that suggest where to look.  For example,
there's the Linux Filesystem Hiearchy Standard 3.0:
https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s04.html

In short, you want to hard code /bin for a command like rm.  And yes it
will always be in /bin on any standard Linux OS.

Despite modern distros making /bin and /usr/bin the same directory, if
the target OS is anywhere close to the standard, you can always find the
basic commands in /bin.  I would not hard code any script to use
/usr/bin for any basic commands and I would not use anything other than
/bin/sh or /bin/bash as the shell script shebang if you want any sort of
portability.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-13 Thread Jon Ribbens via Python-list
On 2022-10-12, Paulo da Silva  wrote:
> Às 22:38 de 12/10/22, Jon Ribbens escreveu:
>> On 2022-10-12, Jon Ribbens  wrote:
>>> On 2022-10-12, Paulo da Silva  wrote:
 Às 19:14 de 12/10/22, Jon Ribbens escreveu:
> On 2022-10-12, Paulo da Silva  
> wrote:
>> Às 05:00 de 12/10/22, Paulo da Silva escreveu:
>>> Hi!
>>>
>>> The simple question: How do I find the full path of a shell command
>>> (linux), i.e. how do I obtain the corresponding of, for example,
>>> "type rm" in command line?
>>>
>>> The reason:
>>> I have python program that launches a detached rm. It works pretty well
>>> until it is invoked by cron! I suspect that for cron we need to specify
>>> the full path.
>>> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
>>> What about other commands?
>>>
>> Thank you all who have responded so far.
>> I think that the the suggestion of searching the PATH env seems the best.
>> Another thing that I thought of is that of the 'which', but, to avoid
>> the mentioned recurrent problem of not knowing where 'which' is I would
>> use 'type' instead. 'type' is a bash (sh?) command.
>
> If you're using subprocess.run / subprocess.Popen then the computer is
> *already* searching PATH for you.
 Yes, and it works out of cron.
> Your problem must be that your cron
> job is being run without PATH being set, perhaps you just need to edit
> your crontab to set PATH to something sensible.
 I could do that, but I am using /etc/cron.* for convenience.

> Or just hard-code your
> program to run '/bin/rm' explicitly, which should always work (unless
> you're on Windows, of course!)
 It can also be in /bin, at least.
>>>
>>> I assume you mean /usr/bin. But it doesn't matter. As already
>>> discussed, even if 'rm' is in /usr/bin, it will be in /bin as well
>>> (or /usr/bin and /bin will be symlinks to the same place).
>>>
 A short idea is to just check /bin/rm and /usr/bin/rm, but I prefer
 searching thru PATH env. It only needs to do that once.
>>>
>>> I cannot think of any situation in which that will help you. But if for
>>> some reason you really want to do that, you can use the shutil.which()
>>> function from the standard library:
>>>
>>>  >>> import shutil
>>>  >>> shutil.which('rm')
>>>  '/usr/bin/rm'
>> 
>> Actually if I'm mentioning shutil I should probably mention
>> shutil.rmtree() as well, which does the same as 'rm -r', without
>> needing to find or run any other executables.
> Except that you can't have parallel tasks, at least in an easy way.
> Using Popen I just launch rm's and end the script.

[threading.Thread(target=shutil.rmtree, args=(item,)).start()
for item in items_to_delete]

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-13 Thread Eli the Bearded
In comp.lang.python, jkn   wrote:
> On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:
>> I'm afraid you will have to look for the command in every path listed in 
>> the PATH environment variable.
> erm, or try 'which rm' ?

It is so hilarious seeing the responses to this thread. Hint: what do
you think the `which` program does?

$ touch /var/tmp/rm
$ chmod 755 /var/tmp/rm
$ env -i PATH=/etc:/usr:/lib:/var/tmp:/tmp /usr/bin/which rm
/var/tmp/rm
$ mv /var/tmp/rm /tmp/ 
$ env -i PATH=/etc:/usr:/lib:/var/tmp:/tmp /usr/bin/which rm
/tmp/rm
$ 

Elijah
--
/usr/bin/busybox rm /tmp/rm
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-13 Thread Mats Wichmann

On 10/12/22 14:51, Paulo da Silva wrote:

Às 19:14 de 12/10/22, Jon Ribbens escreveu:
On 2022-10-12, Paulo da Silva  
wrote:

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command
(linux), i.e. how do I obtain the corresponding of, for example,
"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
What about other commands?


Thank you all who have responded so far.
I think that the the suggestion of searching the PATH env seems the 
best.

Another thing that I thought of is that of the 'which', but, to avoid
the mentioned recurrent problem of not knowing where 'which' is I would
use 'type' instead. 'type' is a bash (sh?) command.


If you're using subprocess.run / subprocess.Popen then the computer is
*already* searching PATH for you.

Yes, and it works out of cron.

Your problem must be that your cron
job is being run without PATH being set, perhaps you just need to edit
your crontab to set PATH to something sensible.

I could do that, but I am using /etc/cron.* for convenience.


Or just hard-code your
program to run '/bin/rm' explicitly, which should always work (unless
you're on Windows, of course!)

It can also be in /bin, at least.
A short idea is to just check /bin/rm and /usr/bin/rm, but I prefer 
searching thru PATH env. It only needs to do that once.


I've read quite a bit of this thread without completely understanding 
the actual problem.


Crontabs have always been good at exposing the problem of "is my program 
expecting something to be set up which isn't guaranteed?" - usually 
environment variables, of which PATH is one. "But it worked from my 
login shell".


Python's standard library has a tool for you:

shutil.which = which(cmd, mode=1, path=None)
Given a command, mode, and a PATH string, return the path which
conforms to the given mode on the PATH, or None if there is no such
file.

`mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
of os.environ.get("PATH"), or can be overridden with a custom search
path.



Since cron usually runs in a minimal environment, you should be able to 
use this to supply a decent value for PATH, look up the command you 
need, and then use that path to launch the command - or fail in some 
sort of graceful way if not found, so you can hear about it and make 
corrections.





--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command [POSTPONED]

2022-10-12 Thread Cameron Simpson

On 13Oct2022 03:25, Paulo da Silva  wrote:
There is another problem involved. The script, works fine except when 
launched by cron! Why?


Record the script output:

# record all output
exec >/tmp/script.$$.out 2>&1
# dump the envionment
env | sort
# turn on execution tracing
set -x
... rest of the script

and have a look afterwards. Cron's environment is very minimal. This 
will show you what's in it.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command [POSTPONED]

2022-10-12 Thread Paulo da Silva

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well 
until it is invoked by cron! I suspect that for cron we need to specify 
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?



For now I will postpone this problem.
I just wrote a small script to delete a dir using rm and it works even 
under cron!
There is another problem involved. The script, works fine except when 
launched by cron! Why?

I'll have to look into this later when I have more time.
For now I solved the problem using a complementary shell script.

Thank you very much to those who responded.
Paulo


--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread rbowman

On 10/12/22 09:06, Chris Green wrote:

Michael F. Stemper  wrote:

On 12/10/2022 07.20, Chris Green wrote:

jak  wrote:

Il 12/10/2022 09:40, jkn ha scritto:

On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:



I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


erm, or try 'which rm' ?


You might but if you don't know where the 'rm' command is, you will have
the same difficulty in using 'which' command. Do not you think?

  From a command prompt use the bash built-in 'command' :-

  command -v rm

... and rm will just about always be in /usr/bin.


On two different versions of Ubuntu, it's in /bin.


I think you'll find it's in both /bin and /usr/bin, usually /usr/bin
is earlier in the path so /usr/bin/rm is the one that will normally be
found first.

It's only in /bin/rm in case one has a system which mounts /bin
separately and earlier in the boot sequence and rm is one of the
commands needed early on.



ls -l /bin
lrwxrwxrwx 1 root root 7 Sep 23 01:41 /bin -> usr/bin



--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Joe Pfeiffer
Cameron Simpson  writes:

> On 12Oct2022 20:54, Jon Ribbens  wrote:
>>On 2022-10-12, Jon Ribbens  wrote:
>>> On 2022-10-12, Joe Pfeiffer  wrote:
 Jon Ribbens  writes:
> on Amazon Linux:
>
> $ which rm
> /usr/bin/rm
> $ sudo which rm
> /bin/rm

 Have some major Linux distributions not done usrmerge yet?  For any that
 have, /bin is a symbolic link to /usr/bin
>
> The above example may just be a different ordering in $PATH.

Sure, but the point is OP can use either /bin/rm or /usr/bin/rm on the
vast majority of systems and execute the same command.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Paulo da Silva

Às 22:38 de 12/10/22, Jon Ribbens escreveu:

On 2022-10-12, Jon Ribbens  wrote:

On 2022-10-12, Paulo da Silva  wrote:

Às 19:14 de 12/10/22, Jon Ribbens escreveu:

On 2022-10-12, Paulo da Silva  wrote:

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command
(linux), i.e. how do I obtain the corresponding of, for example,
"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
What about other commands?


Thank you all who have responded so far.
I think that the the suggestion of searching the PATH env seems the best.
Another thing that I thought of is that of the 'which', but, to avoid
the mentioned recurrent problem of not knowing where 'which' is I would
use 'type' instead. 'type' is a bash (sh?) command.


If you're using subprocess.run / subprocess.Popen then the computer is
*already* searching PATH for you.

Yes, and it works out of cron.

Your problem must be that your cron
job is being run without PATH being set, perhaps you just need to edit
your crontab to set PATH to something sensible.

I could do that, but I am using /etc/cron.* for convenience.


Or just hard-code your
program to run '/bin/rm' explicitly, which should always work (unless
you're on Windows, of course!)

It can also be in /bin, at least.


I assume you mean /usr/bin. But it doesn't matter. As already
discussed, even if 'rm' is in /usr/bin, it will be in /bin as well
(or /usr/bin and /bin will be symlinks to the same place).


A short idea is to just check /bin/rm and /usr/bin/rm, but I prefer
searching thru PATH env. It only needs to do that once.


I cannot think of any situation in which that will help you. But if for
some reason you really want to do that, you can use the shutil.which()
function from the standard library:

 >>> import shutil
 >>> shutil.which('rm')
 '/usr/bin/rm'


Actually if I'm mentioning shutil I should probably mention
shutil.rmtree() as well, which does the same as 'rm -r', without
needing to find or run any other executables.

Except that you can't have parallel tasks, at least in an easy way.
Using Popen I just launch rm's and end the script.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Peter J. Holzer
On 2022-10-12 21:51:39 +0100, Paulo da Silva wrote:
> Às 19:14 de 12/10/22, Jon Ribbens escreveu:
> > If you're using subprocess.run / subprocess.Popen then the computer is
> > *already* searching PATH for you.
> Yes, and it works out of cron.
> > Your problem must be that your cron
> > job is being run without PATH being set, perhaps you just need to edit
> > your crontab to set PATH to something sensible.

The PATH in cron includes the directories with standard commands like
"rm" by default. If a cron job doesn't find rm, either
 * rm isn't where it is supposed to be, or
 * PATH has been changed.

> I could do that, but I am using /etc/cron.* for convenience.

That may be a clue. Does any of the /etc/cron.* files set PATH to weird
value?

In any case I find that when debugging such problems it is helpful to
find out what the actual environment is. A simple 

* * * * * nobody echo $PATH >> /tmp/path.$$

should do that.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Cameron Simpson

On 12Oct2022 20:54, Jon Ribbens  wrote:

On 2022-10-12, Jon Ribbens  wrote:

On 2022-10-12, Joe Pfeiffer  wrote:

Jon Ribbens  writes:

on Amazon Linux:

$ which rm
/usr/bin/rm
$ sudo which rm
/bin/rm


Have some major Linux distributions not done usrmerge yet?  For any that
have, /bin is a symbolic link to /usr/bin


The above example may just be a different ordering in $PATH.


I have immediate access to CentOS 7, Ubuntu 20, and Amazon Linux 2,
and none of those have done that.


Sorry, in fact they have done that - I misread your comment as being
that they had symlinked the executables not the directories. This seems
quite an unwise move to me but presumably they've thought it through.


I think that modern discs are so large these days that the scenario of 
having a small critical /bin with a larger less critical /usr/bin on 
another partition are behind us except in very niche circumstances.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Cameron Simpson

On 13Oct2022 08:50, Cameron Simpson  wrote:

On 12Oct2022 15:16, Dan Sommers <2qdxy4rzwzuui...@potatochowder.com> wrote:

rm doesn't take that long.  Why are you detaching them?


[...]
For remove filesystems, even a local to your LAN NAS, it can take quite 
a while.


Of course I meant "remote" here, not "remove".

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command (shutil.which)

2022-10-12 Thread Eryk Sun
On 10/12/22, Weatherby,Gerard  wrote:
>
> When no path is specified, the results of
> os.environ() are used,
> returning either the “PATH” value or a fallback of
> os.defpath.

The documentation is out of sync with what the code actually does.
os.defpath is hard coded in posixpath and ntpath, but shutil.which()
actually prefers to query the system's default path to use via
os.confstr(), if it's supported:

if path is None:
path = os.environ.get("PATH", None)
if path is None:
try:
path = os.confstr("CS_PATH")
except (AttributeError, ValueError):
# os.confstr() or CS_PATH is not available
path = os.defpath

As documented by POSIX, the CS_PATH string "can be used as a value of
the PATH environment variable that accesses all of the standard
utilities of POSIX.1-2017, that are provided in a manner accessible
via the exec family of functions".

https://pubs.opengroup.org/onlinepubs/9699919799/functions/confstr.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Jon Ribbens via Python-list
On 2022-10-12, Jon Ribbens  wrote:
> On 2022-10-12, Paulo da Silva  wrote:
>> Às 19:14 de 12/10/22, Jon Ribbens escreveu:
>>> On 2022-10-12, Paulo da Silva  wrote:
 Às 05:00 de 12/10/22, Paulo da Silva escreveu:
> Hi!
>
> The simple question: How do I find the full path of a shell command
> (linux), i.e. how do I obtain the corresponding of, for example,
> "type rm" in command line?
>
> The reason:
> I have python program that launches a detached rm. It works pretty well
> until it is invoked by cron! I suspect that for cron we need to specify
> the full path.
> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
> What about other commands?
>
 Thank you all who have responded so far.
 I think that the the suggestion of searching the PATH env seems the best.
 Another thing that I thought of is that of the 'which', but, to avoid
 the mentioned recurrent problem of not knowing where 'which' is I would
 use 'type' instead. 'type' is a bash (sh?) command.
>>> 
>>> If you're using subprocess.run / subprocess.Popen then the computer is
>>> *already* searching PATH for you.
>> Yes, and it works out of cron.
>>> Your problem must be that your cron
>>> job is being run without PATH being set, perhaps you just need to edit
>>> your crontab to set PATH to something sensible.
>> I could do that, but I am using /etc/cron.* for convenience.
>>
>>> Or just hard-code your
>>> program to run '/bin/rm' explicitly, which should always work (unless
>>> you're on Windows, of course!)
>> It can also be in /bin, at least.
>
> I assume you mean /usr/bin. But it doesn't matter. As already
> discussed, even if 'rm' is in /usr/bin, it will be in /bin as well
> (or /usr/bin and /bin will be symlinks to the same place).
>
>> A short idea is to just check /bin/rm and /usr/bin/rm, but I prefer 
>> searching thru PATH env. It only needs to do that once.
>
> I cannot think of any situation in which that will help you. But if for
> some reason you really want to do that, you can use the shutil.which()
> function from the standard library:
>
> >>> import shutil
> >>> shutil.which('rm')
> '/usr/bin/rm'

Actually if I'm mentioning shutil I should probably mention
shutil.rmtree() as well, which does the same as 'rm -r', without
needing to find or run any other executables.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Cameron Simpson

On 12Oct2022 15:16, Dan Sommers <2qdxy4rzwzuui...@potatochowder.com> wrote:

rm doesn't take that long.  Why are you detaching them?


For big things it takes quite a while. On slow discs it can take quite a 
while. For directory trees with many files it can take quite a while.  
For remove filesystems, even a local to your LAN NAS, it can take quite 
a while.


Certainly I've got real world modern day things right now where "quite a 
while" can be many minutes.


I've got an `rmr` script of my own whose entire function is to rename 
the target to something like `.frm.blah` and then remove `.frm.blah` in 
the background. I use it a _lot_.


Ancient anecdote. We got funky new workstations at Uni long ago. For the 
first time in my life "rm filename" came back to the command prompt with 
no perceptable delay. So fast that I first wondered if it had worked at 
all. Previously we'd been using shared minicomputers (PDPs, Vaxen).


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command (shutil.which)

2022-10-12 Thread Weatherby,Gerard
Not going to do any good if it’s not on the PATH in the first place:

https://docs.python.org/3/library/shutil.html

shutil.which(cmd, mode=os.F_OK | os.X_OK, path=None)
Return the path to an executable which would be run if the given cmd was 
called. If no cmd would be called, return None.
mode is a permission mask passed to 
os.access()<https://docs.python.org/3/library/os.html#os.access>, by default 
determining if the file exists and executable.
When no path is specified, the results of 
os.environ()<https://docs.python.org/3/library/os.html#os.environ> are used, 
returning either the “PATH” value or a fallback of 
os.defpath<https://docs.python.org/3/library/os.html#os.defpath>.


From: Python-list  on 
behalf of Thomas Passin 
Date: Wednesday, October 12, 2022 at 5:39 PM
To: python-list@python.org 
Subject: Re: Find the path of a shell command
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

On 10/12/2022 12:00 AM, Paulo da Silva wrote:
> Hi!
>
> The simple question: How do I find the full path of a shell command
> (linux), i.e. how do I obtain the corresponding of, for example,
> "type rm" in command line?
>
> The reason:
> I have python program that launches a detached rm. It works pretty well
> until it is invoked by cron! I suspect that for cron we need to specify
> the full path.
> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
> What about other commands?
>
> Thanks for any comments/responses.
> Paulo
>

Python has a command that usually does the job:

import shutil, os
executable_path = shutil.which(exename, os.X_OK)

If you know that the executable you want is on some non-standard path,
you can or them together:

executable_path = shutil.which(exename, os.X_OK)\
or shutil.which(exename, os.X_OK, other_path)

Presumably the shutil command uses which behind the scenes, but that
doesn't matter.

Now you can include this location when you run the executable from
Python.  If you need to run a system command from a batch file and not
from Python, you will either need to have the right path in effect when
the batch file is run, or manually include the full path to the file in
the batch file.

BTW, on Linux Mint, which is derived from Ubuntu and Debian, rm is at

$ which rm
/bin/rm



--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!lyWawGnR8ddR5pCSS5bC09WInoHss_eleSRXxsjbIL_ndHz2TPW246oagSmaBVekZyVjzO7zKsGqN9NpLtM$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!lyWawGnR8ddR5pCSS5bC09WInoHss_eleSRXxsjbIL_ndHz2TPW246oagSmaBVekZyVjzO7zKsGqN9NpLtM$>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Cameron Simpson

On 12Oct2022 17:49, Paulo da Silva  wrote:

Às 05:00 de 12/10/22, Paulo da Silva escreveu:
I think that the the suggestion of searching the PATH env seems the 
best.


I just want to note that you want to not just check for existence of the 
path, but that it is executable (permissionwise). You want to use `X_OK` 
with the `os.access` function:

https://docs.python.org/3/library/os.html#os.access

Also notice that the `shutil` module has a `which()` function:
https://docs.python.org/3/library/shutil.html#shutil.which

Another thing that I thought of is that of the 'which', but, to avoid 
the mentioned recurrent problem of not knowing where 'which' is I 
would use 'type' instead.


`which` will almost always be in `/usr/bin` (or `/bin` on some systems I 
suppose). But do you need to know where `which` is? If you're invoking 
it instead of search `$PATH`, the normal executtion stuff itself search 
`$PATH`, an will find `which` for you :-)



'type' is a bash (sh?) command.


Yeah. Bash, ksh etc have `type`. It is a builtin, shich can see if a 
command word you give it is a shell alias or function or builtin or 
externally executed command. As you'd imagine, an external executable 
like `which` has no access to the internals of your shell, and can only 
look for executables.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Thomas Passin

On 10/12/2022 12:00 AM, Paulo da Silva wrote:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well 
until it is invoked by cron! I suspect that for cron we need to specify 
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?


Thanks for any comments/responses.
Paulo



Python has a command that usually does the job:

import shutil, os
executable_path = shutil.which(exename, os.X_OK)

If you know that the executable you want is on some non-standard path, 
you can or them together:


executable_path = shutil.which(exename, os.X_OK)\
   or shutil.which(exename, os.X_OK, other_path)

Presumably the shutil command uses which behind the scenes, but that 
doesn't matter.


Now you can include this location when you run the executable from 
Python.  If you need to run a system command from a batch file and not 
from Python, you will either need to have the right path in effect when 
the batch file is run, or manually include the full path to the file in 
the batch file.


BTW, on Linux Mint, which is derived from Ubuntu and Debian, rm is at

$ which rm
/bin/rm



--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Jon Ribbens via Python-list
On 2022-10-12, Paulo da Silva  wrote:
> Às 19:14 de 12/10/22, Jon Ribbens escreveu:
>> On 2022-10-12, Paulo da Silva  wrote:
>>> Às 05:00 de 12/10/22, Paulo da Silva escreveu:
 Hi!

 The simple question: How do I find the full path of a shell command
 (linux), i.e. how do I obtain the corresponding of, for example,
 "type rm" in command line?

 The reason:
 I have python program that launches a detached rm. It works pretty well
 until it is invoked by cron! I suspect that for cron we need to specify
 the full path.
 Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
 What about other commands?

>>> Thank you all who have responded so far.
>>> I think that the the suggestion of searching the PATH env seems the best.
>>> Another thing that I thought of is that of the 'which', but, to avoid
>>> the mentioned recurrent problem of not knowing where 'which' is I would
>>> use 'type' instead. 'type' is a bash (sh?) command.
>> 
>> If you're using subprocess.run / subprocess.Popen then the computer is
>> *already* searching PATH for you.
> Yes, and it works out of cron.
>> Your problem must be that your cron
>> job is being run without PATH being set, perhaps you just need to edit
>> your crontab to set PATH to something sensible.
> I could do that, but I am using /etc/cron.* for convenience.
>
>> Or just hard-code your
>> program to run '/bin/rm' explicitly, which should always work (unless
>> you're on Windows, of course!)
> It can also be in /bin, at least.

I assume you mean /usr/bin. But it doesn't matter. As already
discussed, even if 'rm' is in /usr/bin, it will be in /bin as well
(or /usr/bin and /bin will be symlinks to the same place).

> A short idea is to just check /bin/rm and /usr/bin/rm, but I prefer 
> searching thru PATH env. It only needs to do that once.

I cannot think of any situation in which that will help you. But if for
some reason you really want to do that, you can use the shutil.which()
function from the standard library:

>>> import shutil
>>> shutil.which('rm')
'/usr/bin/rm'

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Weatherby,Gerard
Some thoughts / notes:
1. On our Ubuntu-Server 20.04.3 based systems, /bin/rm and /usr/bin/rm are hard 
links to the same file.

2. Put the following in a bash script and run it from cron. Then you can see 
what your environment is. Mine has PATH=/usr/bin:/bin in it.

#!/bin/bash
env > /tmp/env$$

3. You can background Python just as well as you can a separate rm invocation.
if os.fork() == 0:
os.unlink(….)








From: Python-list  on 
behalf of Grant Edwards 
Date: Wednesday, October 12, 2022 at 5:03 PM
To: python-list@python.org 
Subject: Re: Find the path of a shell command
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

On 2022-10-12, 2qdxy4rzwzuui...@potatochowder.com 
<2qdxy4rzwzuui...@potatochowder.com> wrote:
> On 2022-10-12 at 17:43:18 +0100, Paulo da Silva 
>  wrote:
>>
>> > Probably you could use os.unlink[1] with no problem.
>>
>> No, because I need to launch several rm's that keep running after the script
>> ends.
>
> rm doesn't take that long.

Recursive ones can take a long time.

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!ny8-fSGJEE3wk-Yq188xuT5aamgIUVI6SJQHkNaxMp11JelflHEJ2SQcYqqSC1s8pxkifVVdFyeEcbSv1ZJlqoaxuG_m$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!ny8-fSGJEE3wk-Yq188xuT5aamgIUVI6SJQHkNaxMp11JelflHEJ2SQcYqqSC1s8pxkifVVdFyeEcbSv1ZJlqoaxuG_m$>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Paulo da Silva

Às 20:09 de 12/10/22, Antoon Pardon escreveu:



Op 12/10/2022 om 18:49 schreef Paulo da Silva:

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty 
well until it is invoked by cron! I suspect that for cron we need to 
specify the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?



Thank you all who have responded so far.
I think that the the suggestion of searching the PATH env seems the best.


I fear that won't work.

If it doesn't work in cron, that probably means, PATH is not set 
properly in your cron environment. And if PATH is not set properly, 
searching it explicitely won't work either.



It seems you are right :-( I didn't think of that!
Does 'type' bash command work? I don't know how bash 'type' works.
I need to do some tests.
Thanks
Paulo



--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 20:19, MRAB ha scritto:

On 2022-10-12 06:11, jak wrote:

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty 
well until it is invoked by cron! I suspect that for cron we need to 
specify the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?


Thanks for any comments/responses.
Paulo



I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


Isn't that what the "whereis" command does?


"whereis" does more than the OP wants. A suitable command could be
"which" but if the OP is in a context where he needs to know the path
of the command, then it will have the same problem with "whereis" or
"which". In any case they would be a few lines of Python:

import os

def which(c):
for p in os.getenv('PATH').split(os.path.pathsep):
if p and os.path.isfile(f := os.path.join(p, c)):
return f
return ''

cmd = 'explorer.exe'
cmdp = which(cmd)

if cmdp:
print("found:", cmdp)
else:
print("not found"

--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Jon Ribbens via Python-list
On 2022-10-12, Jon Ribbens  wrote:
> On 2022-10-12, Joe Pfeiffer  wrote:
>> Jon Ribbens  writes:
>>
>>> On 2022-10-12, Michael F. Stemper  wrote:
 On 12/10/2022 07.20, Chris Green wrote:
> ... and rm will just about always be in /usr/bin.

 On two different versions of Ubuntu, it's in /bin.
>>>
>>> It will almost always be in /bin in any Unix or Unix-like system,
>>> because it's one of the fundamental utilities that may be vital in
>>> fixing the system when it's booted in single-user mode and /usr may
>>> not be available. Also, the Filesystem Hierarchy Standard *requires*
>>> it to be in /bin.
>>>
>>> Having said that, nothing requires it not to be elsewhere *as well*,
>>> and in Ubuntu and other Linux systems it is in /usr/bin too. And because
>>> PATH for non-root users will usually contain /usr/bin before /bin (or
>>> indeed may not contain /bin at all), 'command -v rm' or 'which rm' will
>>> usually list the version of rm that is in /usr/bin.
>>>
>>> e.g. on Amazon Linux:
>>>
>>> $ which rm
>>> /usr/bin/rm
>>> $ sudo which rm
>>> /bin/rm
>>
>> Have some major Linux distributions not done usrmerge yet?  For any that
>> have, /bin is a symbolic link to /usr/bin
>
> I have immediate access to CentOS 7, Ubuntu 20, and Amazon Linux 2,
> and none of those have done that.

Sorry, in fact they have done that - I misread your comment as being
that they had symlinked the executables not the directories. This seems
quite an unwise move to me but presumably they've thought it through.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Paulo da Silva

Às 19:14 de 12/10/22, Jon Ribbens escreveu:

On 2022-10-12, Paulo da Silva  wrote:

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command
(linux), i.e. how do I obtain the corresponding of, for example,
"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
What about other commands?


Thank you all who have responded so far.
I think that the the suggestion of searching the PATH env seems the best.
Another thing that I thought of is that of the 'which', but, to avoid
the mentioned recurrent problem of not knowing where 'which' is I would
use 'type' instead. 'type' is a bash (sh?) command.


If you're using subprocess.run / subprocess.Popen then the computer is
*already* searching PATH for you.

Yes, and it works out of cron.

Your problem must be that your cron
job is being run without PATH being set, perhaps you just need to edit
your crontab to set PATH to something sensible.

I could do that, but I am using /etc/cron.* for convenience.


Or just hard-code your
program to run '/bin/rm' explicitly, which should always work (unless
you're on Windows, of course!)

It can also be in /bin, at least.
A short idea is to just check /bin/rm and /usr/bin/rm, but I prefer 
searching thru PATH env. It only needs to do that once.



--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Paulo da Silva

Às 20:16 de 12/10/22, 2qdxy4rzwzuui...@potatochowder.com escreveu:

On 2022-10-12 at 17:43:18 +0100,
Paulo da Silva  wrote:


Às 17:22 de 12/10/22, Tilmann Hentze escreveu:

Paulo da Silva  schrieb:

I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.


Probably you could use os.unlink[1] with no problem.

No, because I need to launch several rm's that keep running after the script
ends.


rm doesn't take that long.  Why are you detaching them?


Because the use of "rm -rf" here is to remove full dirs, mostly in 
external drives, each reaching more than hundreds thousand files.




--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Grant Edwards
On 2022-10-12, 2qdxy4rzwzuui...@potatochowder.com 
<2qdxy4rzwzuui...@potatochowder.com> wrote:
> On 2022-10-12 at 17:43:18 +0100, Paulo da Silva 
>  wrote:
>>
>> > Probably you could use os.unlink[1] with no problem.
>>
>> No, because I need to launch several rm's that keep running after the script
>> ends.
>
> rm doesn't take that long.

Recursive ones can take a long time.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread 2QdxY4RzWzUUiLuE
On 2022-10-12 at 17:43:18 +0100,
Paulo da Silva  wrote:

> Às 17:22 de 12/10/22, Tilmann Hentze escreveu:
> > Paulo da Silva  schrieb:
> > > I have python program that launches a detached rm. It works pretty well
> > > until it is invoked by cron! I suspect that for cron we need to specify
> > > the full path.
> > 
> > Probably you could use os.unlink[1] with no problem.
> No, because I need to launch several rm's that keep running after the script
> ends.

rm doesn't take that long.  Why are you detaching them?

(I'm not debating your point about cron.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Antoon Pardon



Op 12/10/2022 om 18:49 schreef Paulo da Silva:

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty 
well until it is invoked by cron! I suspect that for cron we need to 
specify the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?



Thank you all who have responded so far.
I think that the the suggestion of searching the PATH env seems the best.


I fear that won't work.

If it doesn't work in cron, that probably means, PATH is not set 
properly in your cron environment. And if PATH is not set properly, 
searching it explicitely won't work either.


If rm works when evoked directly in a cron script but not via a python 
script, you may need to export the PATH in your cron script.


--
Antoon Pardon
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread jkn
On Wednesday, October 12, 2022 at 12:07:36 PM UTC+1, jak wrote:
> Il 12/10/2022 09:40, jkn ha scritto: 
> > On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote: 
> >> Il 12/10/2022 06:00, Paulo da Silva ha scritto: 
> >>> Hi! 
> >>> 
> >>> The simple question: How do I find the full path of a shell command 
> >>> (linux), i.e. how do I obtain the corresponding of, for example, 
> >>> "type rm" in command line? 
> >>> 
> >>> The reason: 
> >>> I have python program that launches a detached rm. It works pretty well 
> >>> until it is invoked by cron! I suspect that for cron we need to specify 
> >>> the full path. 
> >>> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
> >>> What about other commands? 
> >>> 
> >>> Thanks for any comments/responses. 
> >>> Paulo 
> >>> 
> >> I'm afraid you will have to look for the command in every path listed in 
> >> the PATH environment variable. 
> > 
> > erm, or try 'which rm' ?
> You might but if you don't know where the 'rm' command is, you will have 
> the same difficulty in using 'which' Command. Do not you think?

I don't need to know where the rm command is in order to use the which command.

I *was* (knowingly and deliberately) assuming that you have a semi-reasonably
working system, and that your question meant "given command X, how do I
find where the executable X is on my system?".

Sounds like you want to make less assumptions than that. Fine. but probably
best to be clear about your assumptions up front.

J^n
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Jon Ribbens via Python-list
On 2022-10-12, Paulo da Silva  wrote:
> Às 05:00 de 12/10/22, Paulo da Silva escreveu:
>> Hi!
>> 
>> The simple question: How do I find the full path of a shell command 
>> (linux), i.e. how do I obtain the corresponding of, for example,
>> "type rm" in command line?
>> 
>> The reason:
>> I have python program that launches a detached rm. It works pretty well 
>> until it is invoked by cron! I suspect that for cron we need to specify 
>> the full path.
>> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
>> What about other commands?
>> 
> Thank you all who have responded so far.
> I think that the the suggestion of searching the PATH env seems the best.
> Another thing that I thought of is that of the 'which', but, to avoid 
> the mentioned recurrent problem of not knowing where 'which' is I would 
> use 'type' instead. 'type' is a bash (sh?) command.

If you're using subprocess.run / subprocess.Popen then the computer is
*already* searching PATH for you. Your problem must be that your cron
job is being run without PATH being set, perhaps you just need to edit
your crontab to set PATH to something sensible. Or just hard-code your
program to run '/bin/rm' explicitly, which should always work (unless
you're on Windows, of course!)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread MRAB

On 2022-10-12 06:11, jak wrote:

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well 
until it is invoked by cron! I suspect that for cron we need to specify 
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?


Thanks for any comments/responses.
Paulo



I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


Isn't that what the "whereis" command does?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Jon Ribbens via Python-list
On 2022-10-12, Joe Pfeiffer  wrote:
> Jon Ribbens  writes:
>
>> On 2022-10-12, Michael F. Stemper  wrote:
>>> On 12/10/2022 07.20, Chris Green wrote:
 ... and rm will just about always be in /usr/bin.
>>>
>>> On two different versions of Ubuntu, it's in /bin.
>>
>> It will almost always be in /bin in any Unix or Unix-like system,
>> because it's one of the fundamental utilities that may be vital in
>> fixing the system when it's booted in single-user mode and /usr may
>> not be available. Also, the Filesystem Hierarchy Standard *requires*
>> it to be in /bin.
>>
>> Having said that, nothing requires it not to be elsewhere *as well*,
>> and in Ubuntu and other Linux systems it is in /usr/bin too. And because
>> PATH for non-root users will usually contain /usr/bin before /bin (or
>> indeed may not contain /bin at all), 'command -v rm' or 'which rm' will
>> usually list the version of rm that is in /usr/bin.
>>
>> e.g. on Amazon Linux:
>>
>> $ which rm
>> /usr/bin/rm
>> $ sudo which rm
>> /bin/rm
>
> Have some major Linux distributions not done usrmerge yet?  For any that
> have, /bin is a symbolic link to /usr/bin

I have immediate access to CentOS 7, Ubuntu 20, and Amazon Linux 2,
and none of those have done that.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Paulo da Silva

Às 17:22 de 12/10/22, Tilmann Hentze escreveu:

Paulo da Silva  schrieb:

I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.


Probably you could use os.unlink[1] with no problem.
No, because I need to launch several rm's that keep running after the 
script ends.



--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Joe Pfeiffer
Jon Ribbens  writes:

> On 2022-10-12, Michael F. Stemper  wrote:
>> On 12/10/2022 07.20, Chris Green wrote:
>>> ... and rm will just about always be in /usr/bin.
>>
>> On two different versions of Ubuntu, it's in /bin.
>
> It will almost always be in /bin in any Unix or Unix-like system,
> because it's one of the fundamental utilities that may be vital in
> fixing the system when it's booted in single-user mode and /usr may
> not be available. Also, the Filesystem Hierarchy Standard *requires*
> it to be in /bin.
>
> Having said that, nothing requires it not to be elsewhere *as well*,
> and in Ubuntu and other Linux systems it is in /usr/bin too. And because
> PATH for non-root users will usually contain /usr/bin before /bin (or
> indeed may not contain /bin at all), 'command -v rm' or 'which rm' will
> usually list the version of rm that is in /usr/bin.
>
> e.g. on Amazon Linux:
>
> $ which rm
> /usr/bin/rm
> $ sudo which rm
> /bin/rm

Have some major Linux distributions not done usrmerge yet?  For any that
have, /bin is a symbolic link to /usr/bin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Paulo da Silva

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well 
until it is invoked by cron! I suspect that for cron we need to specify 
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?



Thank you all who have responded so far.
I think that the the suggestion of searching the PATH env seems the best.
Another thing that I thought of is that of the 'which', but, to avoid 
the mentioned recurrent problem of not knowing where 'which' is I would 
use 'type' instead. 'type' is a bash (sh?) command.


Thanks
Paulo


--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Michael F. Stemper

On 12/10/2022 07.20, Chris Green wrote:

jak  wrote:

Il 12/10/2022 09:40, jkn ha scritto:

On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:



I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


erm, or try 'which rm' ?


You might but if you don't know where the 'rm' command is, you will have
the same difficulty in using 'which' command. Do not you think?

 From a command prompt use the bash built-in 'command' :-

 command -v rm

... and rm will just about always be in /usr/bin.


On two different versions of Ubuntu, it's in /bin.

--
Michael F. Stemper
Psalm 94:3-6
--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Chris Green
Michael F. Stemper  wrote:
> On 12/10/2022 07.20, Chris Green wrote:
> > jak  wrote:
> >> Il 12/10/2022 09:40, jkn ha scritto:
> >>> On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:
> 
>  I'm afraid you will have to look for the command in every path listed in
>  the PATH environment variable.
> >>>
> >>> erm, or try 'which rm' ?
> >>
> >> You might but if you don't know where the 'rm' command is, you will have
> >> the same difficulty in using 'which' command. Do not you think?
> >  From a command prompt use the bash built-in 'command' :-
> > 
> >  command -v rm
> > 
> > ... and rm will just about always be in /usr/bin.
> 
> On two different versions of Ubuntu, it's in /bin.
> 
I think you'll find it's in both /bin and /usr/bin, usually /usr/bin
is earlier in the path so /usr/bin/rm is the one that will normally be
found first.

It's only in /bin/rm in case one has a system which mounts /bin
separately and earlier in the boot sequence and rm is one of the
commands needed early on.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Jon Ribbens via Python-list
On 2022-10-12, Michael F. Stemper  wrote:
> On 12/10/2022 07.20, Chris Green wrote:
>> ... and rm will just about always be in /usr/bin.
>
> On two different versions of Ubuntu, it's in /bin.

It will almost always be in /bin in any Unix or Unix-like system,
because it's one of the fundamental utilities that may be vital in
fixing the system when it's booted in single-user mode and /usr may
not be available. Also, the Filesystem Hierarchy Standard *requires*
it to be in /bin.

Having said that, nothing requires it not to be elsewhere *as well*,
and in Ubuntu and other Linux systems it is in /usr/bin too. And because
PATH for non-root users will usually contain /usr/bin before /bin (or
indeed may not contain /bin at all), 'command -v rm' or 'which rm' will
usually list the version of rm that is in /usr/bin.

e.g. on Amazon Linux:

$ which rm
/usr/bin/rm
$ sudo which rm
/bin/rm
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 14:20, Chris Green ha scritto:

jak  wrote:

Il 12/10/2022 09:40, jkn ha scritto:

On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command
(linux), i.e. how do I obtain the corresponding of, for example,
"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
What about other commands?

Thanks for any comments/responses.
Paulo


I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


erm, or try 'which rm' ?


You might but if you don't know where the 'rm' command is, you will have
the same difficulty in using 'which' command. Do not you think?


 From a command prompt use the bash built-in 'command' :-

 command -v rm

... and rm will just about always be in /usr/bin.



ok but I didn't suggest a very complicated thing:

for p in os.getenv('PATH').split(os.path.pathsep):
if p:
if os.path.isfile(os.path.join(p, 'rm')):
print(f)

--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread Chris Green
jak  wrote:
> Il 12/10/2022 09:40, jkn ha scritto:
> > On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:
> >> Il 12/10/2022 06:00, Paulo da Silva ha scritto:
> >>> Hi!
> >>>
> >>> The simple question: How do I find the full path of a shell command
> >>> (linux), i.e. how do I obtain the corresponding of, for example,
> >>> "type rm" in command line?
> >>>
> >>> The reason:
> >>> I have python program that launches a detached rm. It works pretty well
> >>> until it is invoked by cron! I suspect that for cron we need to specify
> >>> the full path.
> >>> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
> >>> What about other commands?
> >>>
> >>> Thanks for any comments/responses.
> >>> Paulo
> >>>
> >> I'm afraid you will have to look for the command in every path listed in
> >> the PATH environment variable.
> > 
> > erm, or try 'which rm' ?
> 
> You might but if you don't know where the 'rm' command is, you will have
> the same difficulty in using 'which' command. Do not you think?
> 
From a command prompt use the bash built-in 'command' :-

command -v rm

... and rm will just about always be in /usr/bin.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 09:40, jkn ha scritto:

On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command
(linux), i.e. how do I obtain the corresponding of, for example,
"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
What about other commands?

Thanks for any comments/responses.
Paulo


I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


erm, or try 'which rm' ?


You might but if you don't know where the 'rm' command is, you will have
the same difficulty in using 'which' Command. Do not you think?

--
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread jkn
On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:
> Il 12/10/2022 06:00, Paulo da Silva ha scritto: 
> > Hi! 
> > 
> > The simple question: How do I find the full path of a shell command 
> > (linux), i.e. how do I obtain the corresponding of, for example, 
> > "type rm" in command line? 
> > 
> > The reason: 
> > I have python program that launches a detached rm. It works pretty well 
> > until it is invoked by cron! I suspect that for cron we need to specify 
> > the full path. 
> > Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
> > What about other commands? 
> > 
> > Thanks for any comments/responses. 
> > Paulo 
> >
> I'm afraid you will have to look for the command in every path listed in 
> the PATH environment variable.

erm, or try 'which rm' ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well 
until it is invoked by cron! I suspect that for cron we need to specify 
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?


Thanks for any comments/responses.
Paulo



I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.

--
https://mail.python.org/mailman/listinfo/python-list


Find the path of a shell command

2022-10-12 Thread Paulo da Silva

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well 
until it is invoked by cron! I suspect that for cron we need to specify 
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?


Thanks for any comments/responses.
Paulo

--
https://mail.python.org/mailman/listinfo/python-list