My objective is to write a shell script to periodically turn off and on my phone's flashlight (about 1 kHz or 500Hz). My phone is rooted. So I gained superuser shell access (#)

These are the files under my sys/class/leds/flashlight

whyred:/sys/class/leds/flashlight # ls
brightness device max_brightness power subsystem trigger uevent

(Original screenshot of the terminal)

I was able to modify the content of the brightness file and was able to manually switch off and on my flashlight. However, I want to periodically switch on and off with a delay and for that, I need a timer trigger. But within the trigger file above, there is no delay section indicated. There was just some contents within the file (no numbers)

whyred:/sys/class/leds/flashlight # cat trigger
[none] bkl-trigger switch0_trigger flash0_trigger flash1_trigger torch0_trigger torch1_trigger switch1_trigger flash2_trigger torch2_trigger dc-online usb-online main-online pc_port-online battery-charging-or-full battery-charging battery-full battery-charging-blink-full-solid rfkill0 mmc0 mmc1 bms-online parallel-online rfkill1

(Original screenshot of the terminal)

I was able to clock into a period of about 1-3ms by writing a bash script, that echoes brightness 100 followed by sleep and then echoes 0 brightness


while [ 1 le 100]
echo "100" > ~/sys/class/leds/brightness  
echo "100" > ~/sys/class/leds/brightness)
done  # end

But I want at least a 1 kHz frequency, so a timer is the one, sleep function is not advisable.

How can I about attaining my objective?


1 Answer 1


There is Termux and Termux:API.


Android terminal emulator and Linux environment application that works directly with no rooting or setup required. A minimal base system is installed automatically, additional packages are available using the package manager.


This addon exposes device functionality as API to command line programs in Termux.

Install both of them from the F-droid website.

Then open up Termux and run pkg install termux-api nano

Make a shell script with nano using nano torch.sh

Use termux-torch on and termux-torch off to switch on and off the torch.

Also make other edits to fulfill your needs. If you want to do it periodically, use a while loop.

Save and exit the file with Ctrl + X, then type "Y" and enter.

Run cat torch.sh to make sure the shell script is edited successfully.

Make sure the shell script is executable by running chmod +x torch.sh

Finally, run ./torch.sh

P.S. You don't even need root to do that lol

  • Hi box cpm, a fantastic answer actually, i did as you indicated and i was able to control the torch on and off using the above script, like simply within a while loop, i wrote termux-torch on and termux-torch off(no delay between these 2 statements) , and your method works on even non-rooted phones ! lol :} Amazing! But there is one problem, even without me introducing a delay between on and off statements, the period is too high like 0.5 seconds, i want period in range of 1 ms, do you have any other solution to tackle this problem! Apr 19, 2022 at 7:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .