Warn Before Executing Particular Command

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Warn Before Executing Particular Command
# 1  
Old 03-16-2017
Warn Before Executing Particular Command

I'm running CentOS 6.8 and use bash. I would like a warning to appear to the user who runs the command "service httpd restart"

E.g.
Code:
# service httpd restart
are you sure y/n
n
#
(or if y, the command executes).

I looked into it a little but am not sure of the best approach. Aliases I believe are only one word, maybe aliasing the existing 'service' to a script which checks for the 'httpd restart' arguments?

I read shell functions are preferred over aliases, could a shell function do this? I wasn't sure if the shell function went in .bashrc and also needed an alias to work?

I also read something about precmd() and preexec() functionality for bash, but wasn't sure if that would be required, or work, for this case?

Other possibility, modify /sbin/service or /etc/init.d/httpd?

Thanks for any info,

sg

---------- Post updated at 11:49 AM ---------- Previous update was at 08:23 AM ----------

I was able to use this:

Code:
while true; do
    read -p "Are you sure (y/n)?" yn
    case $yn in
        [Yy]* ) stop; start; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

found here -- within the restart case statement in /etc/init.d/httpd, commenting the original stop / start commands -- it seems OK. I still appreciate any suggestion that is considered cleaner.

Thanks,
sg


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 03-16-2017 at 04:11 PM.. Reason: Added CODE tags.
# 2  
Old 03-16-2017
Can anyone run that command on your machine? If true it is a terrible idea. On a reasonably well secured system, only privileged accounts can start and stop services.

I would put a group acl on the service command like maybe admin. Create one or two accounts in that group. Do not let anyone else into the group. Don't give away the password.

Alias-ing the command will not work if some user tries
Code:
/usr/bin/service httpd restart

Same with scripting. You should never make a script redirect for a command.
You will screw up booting the system. Especially if you have to answer a prompt to get it to run. The reboot will just hang.
# 3  
Old 03-17-2017
Thanks for the helpful suggestions Jim!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

I received a WARN when was configuring GNU make,

When I tried to configure GNU make, I received:... WARNING: Your system has neither waitpid() nor wait3(). Without one of these, signal handling is unreliable You should be aware that running GNU make with -j could result in erratic behavior. ... What is that supposed to mean ? my spec: ... (1 Reply)
Discussion started by: abdulbadii
1 Replies

2. Red Hat

Warn Before Executing Particular Command

I'm running CentOS 6.8 and use bash. I would like a warning to appear to the user who runs the command "service httpd restart" E.g. # service httpd restart are you sure y/n n # (or if y, the command executes). I looked into it a little but am not sure of the best approach. Aliases I ... (1 Reply)
Discussion started by: spacegoose
1 Replies

3. AIX

Need help with warn messages in sys logs

Hello, Can you please look into the below warning messages on AIX sys logs ? is this related to application ? do we need to do anything from OS side ? we're running WAS on this LPAR. Feb 12 03:47:48 myserver user:warn|warning IBM Java: JVMJ9VM134W The system fullcore option is set to... (5 Replies)
Discussion started by: System Admin 77
5 Replies

4. Solaris

Role not executing command

Hello Guys, I am studying RBAC. So I create a role called sysadm and gave it the "shutdown" profile. Now when I switch to that role, and execute the shutdown command $ shutdown -y -g0 -i5 The system responds with : shutdown: not found Can anyone help me with this please?... (1 Reply)
Discussion started by: cjashu
1 Replies

5. UNIX for Dummies Questions & Answers

Lot of warn files filling /

hi guys I have suse 11 sp1 and I have a lot of warn file filling / these are under /var/log there's this big one -rw-r----- 1 root root 3.9G Feb 1 10:28 warn warn: ASCII text and the others that are about 2.5 to 3MB - they are about 130 warn-*.bz2 -rw-r----- 1 root root 3.9G Feb... (2 Replies)
Discussion started by: karlochacon
2 Replies

6. UNIX for Dummies Questions & Answers

executing a command using ssh

Hi All, I am trying to execute a command using ssh as below. ssh user123@servername "which ctmcontb" It is gving the error as below no ctmcontb in /usr/bin /usr/sbin /opt/sysadm/bin Not sure from where the PATH is getting picked up. But When I login direclty to the server I am... (5 Replies)
Discussion started by: anilreddy103
5 Replies

7. Programming

#pragma warn codes on Sun Solaris to disable some warns?

I am not able to find warn-codes that should be used in #pragma warn -<code> directive!:wall: Could anybody advise where I can see a list of warnings with codes that (as I understand) should be 3-letters code? I have a pro-C program that produces some warnings. (Do not advise,... (4 Replies)
Discussion started by: alex_5161
4 Replies

8. Solaris

pramga warn codes: where I can see all possible?

(I will not duplicate my post that I create in 'Programming' ( My post ), but the issue also (after C ) is related to Sun Solaris.) I need to find the warning-codes to be used in the #pragma warn.. C-code directives to suppress some compilation warnings. More desciptive explanation you... (2 Replies)
Discussion started by: alex_5161
2 Replies

9. Shell Programming and Scripting

Prepare command before executing

Hi, Couldnt find the right string" to search for a similar question..so dont know if this has been answered yet...problem is that I want to prepare a command with the requisite parameters passed as a string before executing it...eg: the ls command .. I can pass "-l", "-t" as parameters and... (12 Replies)
Discussion started by: harman_6
12 Replies

10. UNIX for Dummies Questions & Answers

Automatically executing a command

Hi I was wondering if anyone knew the answer to this question? I am trying to find a way of executing a command if a certain file is created in the same directory. One way I thought about doing this was to create a FORTRAN program that continually searches for this file. If the file... (8 Replies)
Discussion started by: robbiegregg
8 Replies
Login or Register to Ask a Question