0

I have a simple python code should be run as root. sudo --preserve-env=HOME python3 <file_name> works perfect for me, but, in order not to write this every time, I made the shebang in my code: #!/usr/bin/sudo --preserve-env=HOME python3 — and started it as an usual executable file.

I have made a sudo-shebangs so I didn't expect something to be wrong, but when I started it, it didn't run, but it started to eat RAM. Even the first logging string of code (even before imports) wasn't executed, when it has already eat all the RAM.

Then I remade the shebang to #!/usr/bin/env sudo --preserve-env=HOME python3 (env was added). But then something weird started. This doesn't eat RAM anymore, but CPU... Two cores started some weird dance: the first core is overloaded, the second is resting, then they are being swapped. This repeats regularly and cadencially. Sometimes the second core can be shifted by another one.

What can be the reason of the behavior?

some weird dance

  • There's a limit on the number of arguments in the shebang line. I think you're exceeding it when using `env`. – Barmar Jan 04 '23 at 21:18
  • @Barmar I checked for this: if [this](https://stackoverflow.com/a/10813634/14151848) is to be believed, the limit is 127 chars. I toke sure the shebang does not overflow even including all the path. – ShkiperDesna Jan 04 '23 at 23:36
  • 1
    It's not the length, it's the number of arguments. See https://stackoverflow.com/questions/72121828/usage-of-shebang-in-file-header-is-there-any-option-to-pass-multiple-arguments – Barmar Jan 04 '23 at 23:40
  • You can configure the `sudoers` file to ensure that `sudo python3 ` preserves the `HOME` variable. Really, this kind of configuration doesn't belong in the Python script itself. – chepner Jan 09 '23 at 17:14

0 Answers0