2

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?

2

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 "$@"

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 "$@"
}
  • It didn't worked. Then I prepend the keyword function and done! So: function run_as_webapps(){sg webapps"$*"} did the job. – Lucio May 20 '14 at 3:29
  • @Lucio compromise reached :). See updated answer. – terdon May 20 '14 at 3:35
  • @Lucio damn, sorry I forgot the function which was the whole reason for that edit. Need sleep. Thanks for fixing. – terdon May 20 '14 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.