I am working on a control panel for the inbuilt apache server in OSX/macOS
and am trying to do a check of the status of the server - wether it is running or not. In mac there is no actual single command to check if the server is running through httpd/apachectl (I am using the latter). So I am trying to check by starting the server and then if it gives a return value of 'all good' (or in os.system()'s case, 0) then turn it off again and say it wasn't running, or if it returned an error, say it was already running. Basically I need to get the return value of the bash I am executing inside applescript, and that return value goes into the rest of my python script. This is what I have so far:
status = os.system('''
osascript -e 'do shell script "sudo apachectl start" with administrator privileges'
''')
if (status == 0):
os.system('''
osascript -e 'do shell script "sudo apachectl stop" with administrator privileges'
''')
statusText.config(state=NORMAL)
statusText.insert(END, "Not Running")
statusText.config(state=DISABLED, fg="red")
else:
statusText.config(state=NORMAL)
statusText.insert(END, "Running")
statusText.config(state=DISABLED, fg="green")
I intend to make this a standalone application, so I need all built-ins.
Thank you in advance!
Related
I made a simple script for checking ports with NMAP, which returns 1 if the port is open, and 0 if it is closed. I'm trying to create an item in zabbix that runs this script. In tests, I can run the script successfully through the Zabbix UI, but it returns an empty value after execution. I am able to run the script normally via CLI, I have no permission issues either. I would like some help diagnosing what might be going on.
Here is the script zabbix-nmap.sh
#!/bin/bash
PORT=$(nmap $1 -p $2)
SUB='open'
if [[ "$PORT" == *"$SUB"* ]]; then
echo 1
else
echo 0
fi
Here is the Zabbix Item template configuration/test
Zabbix Item Test
Zabbix Item Config
I tried to run the file as root and as a zabbix user via cli and I can do it normally, via UI the script execution test also does not show an error, but returns an empty result.
I would like to know if it is possible to ignore a shell script command
and continue running the rest of the code, for example if the following
command return me nothing in the terminal and I continue to the next command.
EDIT:
I tried: 2>/dev/null
try
do shell script "kextstat|grep -y appleintel >/tmp/Intel.txt" with administrator privileges
on error
display alert "blabla..."
end try
end showIntel:```
The short version is:
I have a systemd unit that I want to check the return code of a script when I call:
systemctl status service.service
Long version: I had a lsb init script that did exactly that, when status was passed as parameter it called a script that checked the state of several processes and based on the return code the init system returned the state correctly of the software.
Now when adapting the script to systemd I can't find out how to configure this behaviour.
Short answer
This is impossible in systemd. The systemctl status verb always does the same thing, it cannot be overrided per-unit to a custom action.
Long answer
You can write a foo-status.service unit file with Type=oneshot and ExecStart= pointing to your custom status script, and then run systemctl start foo-status. However, this will only provide a zero/nonzero information (any nonzero exit code will be converted to 1).
To get the real exit code of your status script, run systemctl show -pExecMainStatus foo-status, however, if you go this far, then it is simpler to run your script directly.
You can use:
systemctl show -p ExecMainStatus service.service | sed 's/ExecMainStatus=//g'
This will return the exit code of the service.
If you are in control of the code of the service you start / stop that way, then you can easily edit it and save the result in a file.
Otherwise, you can always add a wrapper that does that for you.
#!/bin/sh
/path/to/service and args here
echo $? >/run/service.result
Then your status can be accessed using the contents of that file:
STATUS=`cat /run/service.result`
if test $STATUS = 1
then
echo "An error occurred..."
fi
(Side note: /run/ is only writable by root, use /tmp/ if you are not root.)
I have a problem trying to run shell script via Chef (with docker-provisioning).
This is how I try to execute my script:
bash 'shell_try' do
user "root"
run = "#{some_path_to_script}/my_script.sh some_params"
code " #{run} > stdout.txt 2> stderr.txt"
end
(note that this script should run another scripts, processes and write logs)
Here's no errors in the output, but when I log into machine and run ps aux process isn't running.
I guess something wrong with permissions (or env variables), because when I try the same command manually - it works.
A bash resource just runs the provided script text directly, if you wanted to run a long-running process generally you would set up an Upstart or systemd service and use the service resource to start it.
Finally find a solution (thanks to #coderanger) -
Install supervisor:
Download supervisor cookbook
Add:
include_recipe 'supervisor::default'
Add my service to supervisor:
supervisor_service "name" do
action :enable
#action :start
command '/path/script.sh start'
end
Run supervisor service
All done!
Please see the Chef documentation for your resource: https://docs.chef.io/resource_bash.html. The bash resource does not support a run attribute. Text of the code attribute is run as a bash script. The default action is to run the script unless told otherwise by the resource.
bash 'shell_try' do
user "root"
code " #{run} > stdout.txt 2> stderr.txt"
action :run
end
The code attribute is written to a temporary file where it is then run using the attributes specified in the resource.
The line run = "#{some_path_to_script}/my_script.sh some_params" at this point does nothing.
You have to excuse me if I use the wrong language here of if I'm asking an obvious but that is, after all, why I'm here.
I'm just getting to grips with shell scripting and have written a small script that is "Run as a custom command instead of my shell" to make things a little easier for the things I might want to do. Here's what I've got.
#
# Custom console startup script.
#
path_to_scripts=~/Scripts
echo "Hello $USERNAME, what would you like to do?"
echo "Options:"
echo "-l Continue in local machine"
echo "-s Connect to server"
read response
case $response in
"l") echo "Contunie within local machine.";;
"s") $path_to_scripts/connect_to_server;;
*) echo "Invalid command. Exiting.";;
esac
So my terminal starts up with this script and if I select 's' it runs the 'connect_to_server' script fine and connects then I'm in!
However when I enter an invalid command, or key in 'l' to exit and continue as normal the console says 'The child process exited normally with status 0.'
I know that it has just quit and the script has exited but what I want to do is just run the default shell so that I am then in my local machine at ~, as if id just started up console with default settings. What do I need to run in order to do this?
Run exec "$SHELL" to replace the current process with your normal shell.