Bash – Schedule a Script Job to run automatically but requires user interaction/input

bashcronscripting

I initially started this script for something else, thinking it would be nice for the script to handle the full process automatically, reboot, and start more script stuff. Then I realized I had a problem, as another process I need to run does actually require user input. While I have since solved that secondary problem so it doesn't require user input anymore, I'd still like to know how to do this. I'm fairly new to Linux, been working with it rather intensely with scripting a process over the last month and a half. However, there's still a lot of things I don't know.

So with all that being said, I want to run a script automatically through some method. I've been using crontab and a bash script, but it doesn't necessarily need to be crontab. I usually use this to put the script into crontab from another script, and then write the main script to output a script like the one below the crontab job being inserted.

(crontab -l 2>>/dev/null; echo "* * * * * /home/pi/Public/test2") | crontab

I could care less on how it's done to get the script to appear in a terminal to be interacted with, as long as I have the ability to schedule when that happens. Like on a restart.

#!/bin/bash
echo 'This is a test.'
echo 'This is a written test.' >> /home/pi/Public/testwrite.txt
sleep 10

And yeah, I realize this one goes every minute but it's just for testing purposes to make sure it's working. The scripts I'm using all use "#!/bin/bash" at the top and are given execution rights. It works when started manually and when called in crontab, though the window never appears and instead works in the background. I would like a way to be able to force it into the foreground to either be read or interacted with accordingly.

And yes I've tried googling a variety of things, using them, but I'm not sure I'm using the right terms to get anywhere with it. I'm hoping that someone can point me in the right direction at least, and it'd be the icing on the cake if I was actually shown how to do it with this sample. I hope this is enough information, I'm just needing a general example of how to make this work.

Also, I can't find an interactive tag to put on this question so if someone can do that, it'd be great. 🙂

Edit 01: To be super clear, I'm asking how to make a terminal appear on the desktop via a scheduled script in crontab which runs the script called in crontab.

Edit 02: I'll post this info since it's being asked for, but I like doing things for the sake of the challenge and I'm not looking for a super specific answer. A general how to would be nice to know actually, rather than something specific only to this. However, I initially became concerned with this was I had put this line

sudo openssl req -x509 -newkey rsa:2048 -nodes -keyout /home/drawpileuser/key.pem -out /home/drawpileuser/cert.pem -days 365

into a script to create a self signed certificate for a collaborative drawing program. I wanted to make it accessible to non-tech savvy people to install Drawpile, so I made this super massive script. Though I've since been able to correct that issue by embedding the necessary information into the generated script with

sudo openssl req -x509 -newkey rsa:2048 -nodes -keyout /home/drawpileuser/key.pem -out /home/drawpileuser/cert.pem -days 365 -subj "/C=$country/ST=$state/L=$locality/O=$organization/OU=$unitname/CN=$hostname/emailAddress=$email"

Since I've done that, what I'm now attempting to do is make an easy to use script that on How to install EmulationStation for Raspberry Pi on Raspbian, which runs these commands by doing the first part, and then running the second part after the restart automatically. It then shows the script so it's apparent that work is being done. Nothing fancy here, just knowing that something is going on. I'm also assuming, perhaps incorrectly, that some commands at some point will not be able to be done without user interaction, hence the question of just timing a script to come up on its own with the terminal.

Edit 03 & Answer: User gf_ was able to point me in the right direction. By using the command lxterminal -e command ( Source ), I can make the terminal open a new window. But that's not all that needs to be done( Source 2 ). Specify the path rather than rely on environment variables in crontab… and I was already using PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin so I just went with that. Then create a simple script to use in crontab, which calls another script.

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0
lxterminal -e /home/pi/Public/./test

Crontab (command crontab -e) ends up looking like…

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
* * * * * /home/pi/Public/test2

Which calls the final script test.

#!/bin/bash
echo 'This is a test.'
echo 'This is a written test with lxterminal -e for real.' >>     /home/pi/Public/testwrite.txt
sleep 10

Best Answer

You can combine several bash commands by grouping them with { }

If you want to add a new task to crontab then you need to combine the current crontab config and the new task config. After that send this output as new config to crontab:

{crontab -l 2>/dev/null; echo "* * * * * /home/pi/Public/test2"} | crontab -
Related Topic