0

I run to run a command using the su -s command to start a process. Since I do not want the root user to own the process.

I try to do this by issuing the command

su -s "$CATALINA_HOME/bin/catalina.sh run" tomcat

which returns su: /opt/apache-tomcat/bin/catalina.sh run: No such file or directory

How can I run the su -s command along with arguments to not generate this error?

jgr208
  • 888
  • 14
  • 35

3 Answers3

3

If you're running su as root, you can use -s to specify a different shell (running as root is necessary here since your tomcat user doesn't have a valid shell), and -c to specify the command to run:

su -s /bin/sh -c "$CATALINA_HOME/bin/catalina.sh run" tomcat

You might find start-stop-daemon useful; it has a whole slew of options to specify the user and group to use, how to start the daemon etc. The tomcat8 initscript used in Debian might provide useful inspiration. Or you could look at writing a systemd unit or whatever is appropriate for your system's init.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
3

The -s switch for su command is to change the shell of the specified user. The command you want to run must be preceded by -c switch.

So the command you are looking for is something like this:

su -s /bin/bash -c "$CATALINA_HOME/bin/catalina.sh run" tomcat
MelBurslan
  • 6,836
  • 2
  • 24
  • 35
0

Your other questions at https://stackoverflow.com/questions/37753783/ and Is there any way to tell if a shell script was killed with signal 9 indicate that what's missing from your question is that you are trying to run Tomcat under upstart.

In such a case:

  • start-stop-daemon is not appropriate.
  • Nor is su.
  • Nor are Poor Man's Daemon Supervisor gyrations to do PID file management in shell script.

All of those are attempting to duplicate stuff that upstart does directly. The use of su is particularly bad, because it isn't a tool for dropping privileges. It is a tool for adding privileges, in a login session. It nowadays has involvement under the covers with various login session management subsystems that makes it particularly inappropriate for the (much simpler) task of dropping superuser privileges in a dæmon. start-stop-daemon is just supererogatory. And upstart knows the process IDs of its children.

In the upstart job file, use the setuid and (if appropriate) setgid stanzas to drop privileges to an unprivileged user:

setuid tomcat

And if you do invoke catalina.sh via sh -c, which isn't really necessary, don't wander into systemd House of Horror territory. (It's only marginally less horrendous when these sorts of things are done with upstart.) Do things the daemontools way and have the shell chain to the script, invoking it via the shell's exec command so that upstart sees the dæmon as its direct child process. (And make sure that upstart knows this with the correct expect stanza, too.)

An explicit shell isn't necessary in the first place, though, because upstart can invoke catalina.sh directly itself, just as systemd and other service management tools can. You furthermore thus don't have the worry of ensuring that /bin/bash is actually the right interpreter for that script.

exec $CATALINA_HOME/bin/catalina.sh run

Further reading

JdeBP
  • 66,967
  • 12
  • 159
  • 343
  • I am using RHEL which uses upstart 0.9.4 and does not have the setuid stanza and yes i have already tried. – jgr208 Jun 10 '16 at 19:50
  • Then (a) start providing such information in questions and (b) read the Further Reading section and apply one of the several the tools pointed to there for dropping privileges. – JdeBP Jun 10 '16 at 19:53
  • "upstart script that is used to start apache tomcat on an RHEL 6.8 system" seems to be provided to me. Also this is a upstart so the systemd tools don't apply, the tools mentioned in others are not installed on my system and can not be, I don't have that version of upstart. – jgr208 Jun 10 '16 at 19:58
  • The tools that I just mentioned are *not systemd tools*. And the idea that these tools, many of which have been around for years, are not available for RedHat Linux is [very](https://github.com/kteru/daemontools-rpm) [wrong](https://github.com/imeyer/runit-rpm) [indeed](https://github.com/droyo/rpmbuild). – JdeBP Jun 10 '16 at 20:12
  • never said they were not around i said they can not be installed on the system not by my choice. also some of the stuff is systemd some upstart i never said all. – jgr208 Jun 10 '16 at 20:15