27

If I create a file like:

import os
print os.getlogin()

and run it with cron, I get an exception

print os.getlogin()
OSError: [Errno 22] Invalid argument

If I run it manually in shell -- it works.

Problem is, GitPython 0.3.1 in commit() uses this function, and I need to use it.

Is there any workaround?

I've tested it on Ubuntu10.10/python2.6.6 and Debian5.0.6/python2.5.2.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
gistart
  • 375
  • 1
  • 5
  • 7

4 Answers4

43

From the os.getlogin() docs: "Returns the user logged in to the controlling terminal of the process." Your script does not have a controlling terminal when run from cron. The docs go on to suggest: "For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the login name of the currently effective user id."

Since you don't want to modify GitPython, you could write a script that does this:

import os, pwd

os.getlogin = lambda: pwd.getpwuid(os.getuid())[0]

import git

# do whatever you need to do with GitPython here

I would suggest filing a bug (or better yet, submitting a patch) with GitPython, though.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Yeah, the question was about workarounds) Manually edit foreign package is the last thing i want to do) – gistart Dec 09 '10 at 15:08
  • Ah, OK. I have added a suggestion for addressing that by monkey-patching the `os` module. – kindall Dec 09 '10 at 15:24
  • 7
    a simpler alternative to using the `pwd` module might be [`getpass.getuser()`](https://docs.python.org/2/library/getpass.html#getpass.getuser) – Austin Adams Jul 31 '15 at 02:23
1

Here is an untested guess about a work-around that might work: os.getlogin() calls getlogin() in the C library, which in turn looks up the login name in the utmp record corresponding to the current process. Since there is no utmp record for cron, you could try to create one using

sessreg -a <logname> ; do_stuff ; sessreg -d <logname>

in your crontab. Maybe you have to twiddle around with the sessreg options. And let me know if this really worked if you tried it :)

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 1
    I recently had the same issue in a docker environment, due to the fact that I couldn't change the source code, this workaround helped me solve the problem – user3582076 May 01 '18 at 22:46
0

I had a similar problem with a python program running in docker from python:3.7-alpine. Here's the problem I was seeing:

/app/bin # ./util.py 
Traceback (most recent call last):
  File "./util.py", line 58, in <module>
    __auth__ = '/tmp/util-' + os.getlogin() + '.auth'
OSError: unable to determine login name

I was able to circumvent the problem by exporting LOGNAME.

 docker run -e LOGNAME  -it --entrypoint /bin/sh b7c35f...cef2738 
Mark
  • 4,249
  • 1
  • 18
  • 27
0

Use getpass.getuser()

Available for Unix, Windows. See documentation.

Get the username from the environment or password database.

First try various environment variables, then the password database. This works on Windows as long as USERNAME is set

From Python's os.login() documentation:

Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use getpass.getuser() since the latter checks the environment variables LOGNAME or USERNAME to find out who the user is, and falls back to pwd.getpwuid(os.getuid())[0] to get the login name of the current real user id.


If a library is using it, as in OP's case. As mentioned by @kindall:

import os, getpass

os.getlogin = lambda: getpass.getuser()

# The rest of your code
Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29