# Owner: glow # KeyID: E65A8520F36AEE13CFE4F56BEB8FE331063A4794 # Key: https://glow.li/pgp # Verify: curl "https://glow.li/posts/display-output-of-a-bash-script-on-a-widget/index.html.asc" | gpg -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 +++ { "date": "2016-02-16 00:01", "layout": "post", "oldurls": [ "technology/2016/2/16/display-output-of-a-bash-script-on-a-widget/" ], "tags": ["Technology", "Android", "Homescreen", "Widget", "CLI", "Termux"], "title": "Display output of a bash script on a widget" } +++ [![Screenshot](media/images/termuxwidget1)](media/images/big/termuxwidget1-big) I managed to build a bridge between [Termux](https://termux.com/) and [Minimalistic Text](https://play.google.com/store/apps/details?id=de.devmil.minimaltext) using [Tasker](https://tasker.joaoapps.com/). I use it to display the last 20 lines of the [#termux irc channel](https://gitter.im/termux/termux) as you can see in the picture. I've done it without having Tasker check a file every two minutes. The Task is directly called from the script. _This tutorial is not Termux specific and should work with any terminal emulator_ ### Step 1: Creating the script All you have to do is write your output to a file in `/sdcard/`. In this example I am grabbing the last 20 lines of log of the [#termux irc channel](https://gitter.im/termux/termux) from my server. ``` while true; do # You can basically add your own commands here. They just have to write # their output to `/sdcard/widget` # For maximum battery and bandwidth efficiency tail and cut are # executed on the server # the cut command removes the timestamps # # for this to work you need to have placed your public key on the server. # Otherwise you will need to enter your password every two minutes ssh user@example.com "cat ~/.weechat/logs/irc.freenode.#termux.weechatlog |tail -n 20 | cut -c 21-">/sdcard/widget # This sends an Intent. This is how we'll call the Tasker task which # updates the widget. "net.dinglish.tasker" has to be there every # time for tasker to receive the intent. The last part is the name # of the intent which you can change if you want more than one. # This normally produces a bit of useless output. I am removing # #that with `>/dev/null` am broadcast -a net.dinglish.tasker.updatewidget --user 0 >/dev/null # sleep for 120 seconds. How often it should update the widget. sleep 120 done ``` If you run this it will download the last 20 lines of the log from a server, paste it into `/sdcard/widget` and call Tasker with an Intent. You should also create the file `/sdcard/widget` now. Simply do `touch /sdcard/widget`. ![Screenshot](media/images/termuxwidget2){ .right} ### Step 2: Create a Tasker profile 1. Create a new profile 2. Choose "Event" 3. In the "Select Event Category" dialog choose "System"->"Intent Received" 4. As Action define `net.dinglish.tasker.updatewidget` This profile will now be called whenever it recieves the "updatewidget" intent. This is a usefull way to extend the functionality of Termux. I also use it to kick of some [Foldersync](https://play.google.com/store/apps/details?id=dk.tacit.android.foldersync.lite) jobs from the terminal. ### Step 3: Create the Task ![Screenshot](media/images/termuxwidget3){.right .size-half} 1. Tap "New Task" and give it a name 2. Tap + to add a new Action 3. Choose "File"->"Read File" 4. Tap 🔍 and choose `/sdcard/widget`. 5. Under "To Var" enter `%Widget` 6. Tap + again to add another new Action 7. Choose "Plugin"->"Minimalistic Text"->"Minimalistic Text" 8. Click the pen to edit the configuration 9. Enter `%Widget` for both variable name and variable content ### Step 4: Configure Minimalistic Text 1. Add a Minimalistic Text widget to your homescreen 2. Choose any preset 3. In the "layout" tab remove remove all rows. Now you have a blank widget 4. Add a new element with + 5. Switch "Misc" Tab and tick "Locale variable" 6. Tap "OK" 7. Click on the new "Locale variable" element 8. In the popup enter `%Widget` as a the "Variable name" 9. "OK" and Save ![Screenshot](media/images/termuxwidget5){.inline} ![Screenshot](media/images/termuxwidget4){.inline} ### Step 5: Run the script Now you can run the script in Termux. Your widget should get updated right away. -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEE5lqFIPNq7hPP5PVr64/jMQY6R5QFAmRuqFAACgkQ64/jMQY6 R5QBbQ//bwyiaC/mfxehBF9J8dCbkycucQyeMUtwc5G/yBJMF/YbrGxNacC9JTnO 4Jg99UF50eX1YEtvVvwaVAVXfClDnMKfBhTMqAN4kYsF/mgh9cpL0evZZ/8ZKWMA +GCCtLVYg67hu4BpIhYiC8HzHLzMHCIpt0YramALsGV31rbQ23bmysU1PPiia/aI qHBtE2IeLjLhaKuYDhcVGOUzjLspjq4kHmBwr3ebtIeRKaPEpOXYCNYN02r8KW38 fWoLajuppJUxjS0BeJ/qMy4bjGE68T41gewEwPH91saFctH7CALW/s5/PSI6CMTQ RHaVIZ6JVYjJgun/R+wFRRozA6GBDfQhGk5/eukqDoJTG09BpTe00tdT64+ZzoCo 7PobwZCMJKbkCCaiKvqFbYz2ue6ue7cCQbz9tgh0gZcvQPfj9SIXnX+ogKK+vRRd vqbbQZBmoNSTTJg2U40UWKwMog622bQx3UbWGHXQIEeZmpxMUyhpDVOFi9HeKua7 wIKB+N09sTvOBrhzG7aNM58EYsJkEMCxARF7lzFprIFpE6aSruryWTsoM9I9fxC9 hGJtjo29llQun/cW7Rwp+yuVX058bjkPCeVicP54AwzfoVEYMnbjW2PIvJGQ02Ik 6SSwAcDBIGyXwwP0hEG/hwnnqlOC5d3kUVDXLCcOlQKAKUfGLO0= =DMlm -----END PGP SIGNATURE-----