Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Arch - Periodic script execution


    Hi everyone,

    I just wrote a script for my system. It automatically sets the light levels of my keyboard light.

    This script should run in intervals. I did right now put it in an endless loop (script internally) and start the script with a cron job (cronie) at reboot. This script runs right now every two seconds.

    Well it works and it takes up to .7% of my CPU when executed and constantly .2% of my RAM for being held in RAM. I wonder now if I could do this more efficienly. My first thought was using cron to execute it periodically, but cron's own cicles are somewhere between every 30-60 seconds.

    So how should I best do that?

    Here's the script I use:
    Code:
    #!/usr/bin/python3
    
    import os
    import time
    
    while True:
        ### Input light level (format (x,0) with x = {0..255})
        lightSensor = open("/sys/devices/platform/applesmc.768/light")
        lightInput = lightSensor.readline()
        lightSensor.close()
    
        ### Convert lightInput into an integer
        lightInput = int(lightInput[1:lightInput.index(",")]) #cut out the number and turn it int
    
        if lightInput <= 50:
            lightOut = int(255 - (lightInput * 5.1))
        else:
            lightOut = 0
    
        ### give out the command
        # print("Light level: " + str(lightInput)) #debug
        # print("kbd light is set to: " + str(lightOut)) #debug
    
        os.system("kbdlight set " + str(lightOut))
        time.sleep(2)

  2. #2
    1) make it a shell script (this is so simple, i'd use dash instead of bash)
    2) look into systemd timers.
    I am not a "Linux Guru"! Get off me! The Forum software won't let me change it!
    How to ask smart questions | Don't be a Help Vampire
    You can post a link by removing "http://www." from it.

  3. #3
    I will try this and come back with my results. Thank you for your help

  4. $spacer_open
    $spacer_close
  5. #4
    So, I've transformed the whole thing into a bash script and it got instantly smaller. Only .1% of my RAM is used and only every 5 to 7 seconds it notices a jump to .7% CPU running time.

    I also used systemctl. What a rabbit hole this was. But I see two ways of controlling this script.

    1. I set up my service file to start my script at start up and then once every 2 seconds
    2. I add an endless loop in my bash script with a sleep 2 command and only set the .service up to start it and restart it every time it accidentally gets closed

    Is there any good reason to prefer one way over the other?

  6. #5
    for a 2 second interval, i'd use an endless loop with "sleep 2" inside the script.
    if it was a significantly larger interval, i'd use a systemd timer.
    I am not a "Linux Guru"! Get off me! The Forum software won't let me change it!
    How to ask smart questions | Don't be a Help Vampire
    You can post a link by removing "http://www." from it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •