Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Edited to reflect jonny's comment regarding positioning of - and username options.
Source Link
baumgart
  • 2.6k
  • 18
  • 17

To run your script as another user as one command, run:

/bin/su - postgres -c "/path/to/backup_db.sh /tmp/test" - postgres Breaking it down: /bin/su : switch user -c "/path/to..." : command to run - : option to su, make it a login session (source profile for the user) postgres : user to become -c "/path/to..." : command to run 

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and cause you to use a compromised version of su.

To run your script as another user as one command, run:

/bin/su - postgres -c "/path/to/backup_db.sh /tmp/test" Breaking it down: /bin/su : switch user - : option to su, make it a login session (source profile for the user) postgres : user to become -c "/path/to..." : command to run 

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and cause you to use a compromised version of su.

To run your script as another user as one command, run:

/bin/su -c "/path/to/backup_db.sh /tmp/test" - postgres Breaking it down: /bin/su : switch user -c "/path/to..." : command to run - : option to su, make it a login session (source profile for the user) postgres : user to become 

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and cause you to use a compromised version of su.

Repeated myself in last line.
Source Link
baumgart
  • 2.6k
  • 18
  • 17

To run your script as another user as one command, run:

/bin/su - postgres -c "/path/to/backup_db.sh /tmp/test" Breaking it down: /bin/su : switch user - : option to su, make it a login session (source profile for the user) postgres : user to become -c "/path/to..." : command to run 

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and edit your pathcause you to use a compromised version of su.

To run your script as another user as one command, run:

/bin/su - postgres -c "/path/to/backup_db.sh /tmp/test" Breaking it down: /bin/su : switch user - : option to su, make it a login session (source profile for the user) postgres : user to become -c "/path/to..." : command to run 

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and edit your path to use a compromised version of su.

To run your script as another user as one command, run:

/bin/su - postgres -c "/path/to/backup_db.sh /tmp/test" Breaking it down: /bin/su : switch user - : option to su, make it a login session (source profile for the user) postgres : user to become -c "/path/to..." : command to run 

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and cause you to use a compromised version of su.

Source Link
baumgart
  • 2.6k
  • 18
  • 17

To run your script as another user as one command, run:

/bin/su - postgres -c "/path/to/backup_db.sh /tmp/test" Breaking it down: /bin/su : switch user - : option to su, make it a login session (source profile for the user) postgres : user to become -c "/path/to..." : command to run 

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and edit your path to use a compromised version of su.