Shutdown Script: Difference between revisions

From NURDspace
No edit summary
No edit summary
Line 4: Line 4:
|Tool=No
|Tool=No
}}
}}
A script that shuts PC's down on the space.


The Script
* Make sure you have paho installed.
apt install python-pip
pip install paho-mqtt
* Log in as root and access root folder.
sudo -s
cd /root/
* Enter crontab job.
crontab -e
* Add a crontab job on the last line.
* * * * *  python /home/user/space_status_shutdown.py
 
==The Script==


<pre><nowiki>
<pre><nowiki>
Line 19: Line 31:


# The callback for when the client receives a CONNACK response from the server.
# The callback for when the client receives a CONNACK response from the server.
def  
def on_connect(client, userdata, flags, rc):
on_connect(client, userdata, flags, rc):


     if rc==0:
     if rc==0:
Line 39: Line 50:


# The callback for when a PUBLISH message is received from the server.
# The callback for when a PUBLISH message is received from the server.
def
def on_message(client, userdata, msg):
on_message(client, userdata, msg):


     print(msg.topic+" "+str(msg.payload))
     print(msg.topic+" "+str(msg.payload))
Line 64: Line 74:


client.on_message = on_message
client.on_message = on_message


client.connect("arbiter.vm.nurd.space", 1883, 60)
client.connect("arbiter.vm.nurd.space", 1883, 60)

Revision as of 01:28, 7 March 2019

Shutdown Script
NoPicture.png
Participants
Skills Python, Linux
Status
Niche
Purpose
Tool No
Location
Cost
Tool category

Shutdown Script Property "Tool Image" (as page type) with input value "File:{{{Picture}}}" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process. {{{Picture}}} {{#if:No | [[Tool Owner::{{{ProjectParticipants}}} | }} {{#if:No | [[Tool Cost::{{{Cost}}} | }}

A script that shuts PC's down on the space.

  • Make sure you have paho installed.
apt install python-pip
pip install paho-mqtt
  • Log in as root and access root folder.
sudo -s
cd /root/
  • Enter crontab job.
crontab -e
  • Add a crontab job on the last line.
* * * * *  python /home/user/space_status_shutdown.py

The Script

#!/usr/bin/env python

#2019 The_Niz

#checks space status and shuts machine down if space is closed


import paho.mqtt.client as mqtt
import os 

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):

    if rc==0:

        client.connected_flag=True #set flag

        print("Connected with result code "+str(rc))

    else:

        print("Bad connection Returned code=",rc)

        client.loop_stop()

        client.disconnect()

    client.subscribe("space/state")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):

    print(msg.topic+" "+str(msg.payload))

    if msg.payload=="False":

        print("Space is uit")

        os.system('sudo /bin/systemctl poweroff')

    else:

        print("Space is aan")

    client.loop_stop()

    client.disconnect()


client = mqtt.Client()

client.on_connect = on_connect

client.on_message = on_message

client.connect("arbiter.vm.nurd.space", 1883, 60)

client.loop_forever()