question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When using .sudo within a .cd context, the script breaks, because the cd command is passed to sudo, rather than to the shell, which results in sudo erroring out with sudo: cd: command not found

Currently working around this issue with c.run("sudo [cmd here]"), since I have disabled the password prompt.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:11

github_iconTop GitHub Comments

4reactions
BenSturmfelscommented, Jun 30, 2021

I’ve just been dealing with this issue myself, where I need to run a bunch of sudo commands within the same directory. The workaround I’ve used is to wrap the whole sudo command in a bash -c so that it executes in a shell and repeat the cd for each command:

c.sudo("bash -c 'cd /srv/website && git init --quiet'")
c.sudo("bash -c 'cd /srv/website && git config receive.denyCurrentBranch ignore'")

That’s a bit harder on the eyes, but you could potentially write a function to reduce the repetition.

My understanding is that there’s two related issues here:

  1. sudo() seems to just do simple string concatenation, which isn’t quite enough. For example, if you write sudo('sudo whoami && whoami'), this translates to sudo whoami && whoami, of which only the first command has elevated privileges:
$ sudo whoami && whoami
root
ben

sudo() should probably make the whole command run with elevated privileges, more like this:

$ sudo bash -c 'whoami && whoami'
root
root
  1. cd is a shell built-in, rather than a program on the $PATH. So it can’t actually be run with the above simple string concatenation version of sudo(). For example:
$ sudo cd /tmp
sudo: cd: command not found

This could also be fixed by wrapping with a shell:

$ sudo bash -c 'cd /tmp'

2reactions
golyalphacommented, May 26, 2020

So, we can’t run commands as root (using sudo) in a different directory than our home directory? That doesn’t really make sense, if I’m being entirely honest.

I do understand that every time a c.run or c.sudo call is made, it is essentially run in a new “session”, resulting it it always being run in a new shell.

I also understand that with c.cd() modifies the way the command is constructed such, that cd [dir] && is prefixed to the actual command in c.run.

IMO, all this particular fix would entail is making sure that the cd [dir] && prefix goes before the sudo [cmd], rather than inside (sudo cd [dir] && cmd) like it does now.

As far as I can tell, there shouldn’t be any reason for c.cd and c.sudo to be incompatible with each other.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why won't "sudo cd" work? - Super User
cd is a shell builtin. sudo only works with executables. You could do sudo sh -c 'cd dirname' but as soon as the...
Read more >
How to enter a directory with the 'cd' command if it has 700 ...
sudo cd won't work because the cd command is built into the shell. So you are saying become root and then run this...
Read more >
why cant I sudo cd into /root [duplicate] - Unix Stack Exchange
when I run sudo cd /root it says sudo: cd: command not found but it works perfectly fine if I first sudo su...
Read more >
Why no "sudo cd" - command not found - General Discussion
The whole concept of working directory is a shell-internal thing. Therefore, outside the shell, and in programs like sudo , it has no...
Read more >
Get root permissions to cd into a directory without sudo su/-s
The end result on both systems that have cd as a command and Linux is the same.. you end up on the command...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found