How to refresh privileges on a script?

Tags:

Answer: 1

18 hours ago

I have an script that modify files and group permissions. But it doesn't bring effects 'till I close the session and open a new one.

Instructions where permissions are changed:

sudo groupadd --system webapps
sudo useradd --system --gid webapps --home /home/lucio/server/webapps/hello_django hello
sudo chown -R hello:webapps .
sudo chmod -R g+w .
sudo usermod -a -G webapps `whoami`

Instruction where I get the error:

cd /home/lucio/server/webapps/
cp assets/gunicorn_start.bash.template hello_django/bin/gunicorn_start.bash

The last line gives me the following error:

cp: cannot create regular file 'hello_django/bin/gunicorn_start.bash': Permission denied

This should help:

screenshoot

I have tried using login username after changes privileges but that does absolutely nothing.

How can I update the privileges without exit from the script?

Answer: 2

6 hours ago

I think the best you can do is to run the commands you need to run using sg.

DESCRIPTION
   The sg command works similar to newgrp but accepts a command. The
   command will be executed with the /bin/sh shell. With most shells you
   may run sg from, you need to enclose multi-word commands in quotes.
   Another difference between newgrp and sg is that some shells treat
   newgrp specially, replacing themselves with a new instance of a shell
   that newgrp creates. This doesn't happen with sg, so upon exit from a
   sg command you are returned to your previous group ID.

So, after you have created the group and added yourself to it, you will be able to run commands as a member of that group with sg groupname command. So, for example, this will work:

sudo groupadd --system webapps
sudo useradd --system --gid webapps --home /home/lucio/server/webapps/hello_django hello
sudo chown -R hello:webapps .
sudo chmod -R g+w .
sudo usermod -a -G webapps $(whoami)

## From now on, run all commands through sg
sg webapps "mkdir foorbar"

Since that will be a pain to write, I would put it in a function:

run_as_webapps() sg webapps "[email protected]"

Then, call whatever commands you need to call using that function

run_as_webapps mkdir foobar

And the end of the day though, it might be easier to just run the whole script as root instead. Either that or have two scripts, one to set up the groups and another to do everything else. Just log out and log back in before running the second one.


The POSIX specification clearly states that functions should be defined in this way:

foo () command

For reasons I don't understand, the OP had trouble with this and instead had to use

function run_as_webapps(){
    sg webapps "[email protected]"
}

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9