2

In my makefile there is a task to sync config files

redis:
    mkdir -p /var/lib/redis
    mkdir -p /var/log/redis
    useradd --system --home-dir /var/lib/redis redis
    chown redis.redis /var/lib/redis
    chown redis.redis /var/log/redis
    cp ./scripts/redis-server.d.conf /etc/init/redis-server.conf
    cp ./scripts/redis.conf /etc/redis.conf
    restart redis

but when I run 2nd time:

useradd --system --home-dir /var/lib/redis redis
useradd: user 'redis' already exists

as you can see, it halt on useradd, can I continue run it?

guilin 桂林
  • 17,050
  • 29
  • 92
  • 146
  • possible duplicate of [How to have gnu make continue after error?](http://stackoverflow.com/questions/2188376/how-to-have-gnu-make-continue-after-error) – jcollado Mar 01 '12 at 08:58

1 Answers1

2

The problem is that the useradd command is returning an error code.

You can prepend a dash to the command as explained here (actually, I found this is a duplicated question):

-useradd --system --home-dir /var/lib/redis redis

Alternatively, a workaround to make that command always return a success code is to combine it with true as follows:

useradd --system --home-dir /var/lib/redis redis || true
Community
  • 1
  • 1
jcollado
  • 39,419
  • 8
  • 102
  • 133
  • 1
    It's slightly awkward to assume that any error from `useradd` would be because the user already exists. A better fix would be to check if the user already exists, and only if not, attempt to create it. – tripleee Mar 01 '12 at 10:15