Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-27-2016, 11:24 PM   #1
LQ Newbie
Registered: Apr 2013
Posts: 29

Rep: Reputation: Disabled
Prevent shell script from being run directly using sh command

There is a shell script used to stop/start appserver need to run this script using service file only(service appserver start/stop/status) and prevent direct execution of shell script(./appserver start/stop/status).Is there any utility in linux which can help me out.

Thanks in advance...!!!
Old 06-28-2016, 12:56 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,609

Rep: Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445
If the script file is
will fail because its the wrong name.

If you ensure that there are no 'x' perms set, then even ./ will fail, BUT if its valid bash code, then 'bash ./' will run and you can't stop it even if there are no 'x' perms.

It would help if you explained why you want to do this.
You can certainly make it root owner/grp accessible only, in which case only root can run it (which is what happens with the service cmd..)

Old 06-28-2016, 01:17 AM   #3
LQ Newbie
Registered: Apr 2013
Posts: 29

Original Poster
Rep: Reputation: Disabled
Here is some more context.Sorry my question had few voids.

The script should not be executeable as ./ or "sh" directly by user(tomcat) who is owner of the script but it should be executable by service file(service appserver start/stop/status) as owner of that script(tomcat).I am fairly comfortable with how I can execute script using (service appserver start/stop/status) where I can give User=tomcat in RHEL7 service file.What I am looking disable execution of directly using ./ or "sh"
Old 06-28-2016, 02:46 AM   #4
LQ Addict
Registered: Dec 2013
Posts: 11,308
Blog Entries: 8

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
make it be owned by tomcat, and remove all other permissions?

like: chmod 600 ./; chown tomcat:tomcat ./
Old 06-28-2016, 02:56 AM   #5
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Maybe you could put code IN the script (IF modifying it is a possibility)
to check that the env/parent/arg $0/...(I'm not sure how)
is the service manager [systemd?] and not just a parent shell.

Conceptually, I'm thinking that: IF a userid could see/COPY! a file,
that userid can "perform" whatever the script does!
I think a file/directory can be unreadable by owner (if that might help).

I'm guessing what you want is the script to *immediately exit*
IF the [?]parent="runER"[?] is NOT the [?]system[?] ...

(excuse my blindly trying to help clarify, even tho I don't know specifics)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script - command works directly in command line but not in script raoulcousins Linux - Newbie 6 08-21-2013 07:43 PM
Unable to run another command from within shell script, when run from home directory blackray1 Linux - Newbie 7 07-01-2013 09:42 AM
How to use ssh and run command in the remote machine using shell script salmanmanekia Linux - Newbie 8 04-28-2011 07:41 AM
gnome launcher won't run a sudo command directly or via a script gnreeke Linux - Desktop 1 06-23-2009 10:44 AM
How to run shell script directly without inquiring running mode? Piero Linux - Software 3 09-03-2003 02:28 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration