Link to home
Start Free TrialLog in
Avatar of Matthew Roessner
Matthew Roessner

asked on

Start QSH command from CL

I am attempting to start Redis from a CL program.  Typically, this is the process I use to start Redis:

CALL QP2TERM
CD /QOPENSYS
/QOpensys/opt/freeware/sbin/redis-server  --logfile /redislog/redis-server.log --maxmemory 1GB

I want to do this from a CL instead of manually through command line.  Does anyone know how I can accomplish this?

Any help is much appreciated.
Avatar of Gary Patterson, CISSP
Gary Patterson, CISSP
Flag of United States of America image

Use qp2shell:

http://forums.iprodeveloper.com/forums/aft/49452

Create a little shell script that does this:

CD /QOPENSYS
/QOpensys/opt/freeware/sbin/redis-server  --logfile /redislog/redis-server.log --maxmemory 1GB

Then run  that script by calling qp2shell.
- Gary
Avatar of Matthew Roessner
Matthew Roessner

ASKER

I don't have any experience with creating shell scripts.

I just created something in the /redislog directory. I just did a

EDTF '/redislog/startredis'

Then I pasted in the two commands:

CD /QOPENSYS
/QOpensys/opt/freeware/sbin/redis-server  --logfile /redislog/redis-server.log --maxmemory 1GB

I saved that...and then created a CL with the following:

 CALL       PGM(QP2SHELL) PARM('/redislog/startredis')

This didn't work...  Got the following:

Error loading i5/OS PASE program /redislog/startredis
New files aren't created with execute authority.

Go into PASE,

chmod 0755 /redislog/startredis

To make the script executable, and try it again.
I did the following:

CALL QP2TERM

$ chmod 0755 /redislog/startredis

After doing this, I re-ran it and I again got the following:

i5/OS PASE program or shell script not executable. Path is /redislog/startredis.                                              
Error loading i5/OS PASE program /redislog/startredis.  See previous messages.
To call from qp2shell, you need this as the top line of your script:

#!/usr/bin/sh

Also make sure the script file is in ASCII (CCSID 819 on my US-localized machine), and uses LF as the new line terminator.  This is one way to do it from a QP2TERM:

echo \#!/usr/bin/sh > /redislog/startredis
echo CD /QOPENSYS >> /redislog/startredis
echo /QOpensys/opt/freeware/sbin/redis-server  --logfile /redislog/redis-server.log --maxmemory 1GB >> /redislog/startredis

chmod 0755 /redislog/startredis

Open in new window

I think we are almost there. I am able to start Redis now from my program...but the program never "completes".  Is there any way to "exit" the Qshell?

If I simply call my program:  CALL LIBRARY/REDISSTR - I get an X on my screen and control is never returned.  I see the following in my QBATCH sbs:

STARTREDIS   QSYSOPR     BCH      .0  PGM-sh           THDW
STARTREDIS   QSYSOPR     BCI      .0  PGM-redis-serv   SELW

Basically it looks like the top job (the PGM-sh) has control.  Essentially the same thing happens if I do a SBMJOB.  

Any idea how to get that part to end - but to keep the bottom job running?

I really appreciate all the help!

Matt
Try putting the "source" command or "exec" command in front of the redis-server command:

source /QOpensys/opt/freeware/sbin/redis-server  --logfile /redislog/redis-server.log --maxmemory 1GB

or

exec /QOpensys/opt/freeware/sbin/redis-server  --logfile /redislog/redis-server.log --maxmemory 1GB

"source" says to run the program/script in the current shell instead of spawning a new one.  If the redis-server program ends eventually, the next line of your startredis script would then run, if there was one.

"exec" says to run the program.script IN PLACE OF the current shell.  So control gets transferred to redis-server.  When it ends the shell will end, and if there were more lines in the startredis script they would never be executed.
I attempted to use both EXEC and SOURCE.  When I used either of these and ran my program:

CALL <LIBRARY>/REDISSTR


Nothing happens...  The Redis job does not start (but control is returned...)  Essentially it looks like the command runs but it doesn't do anything
Just to clarify, this is not qshell.  

It is PASE, which defaults to the bash shell.  QP2TERM and QP2SHELL start the PASE environment.  QSH command starts the qshell environment.
OK, so I assume you have a CL called REDISSTR.

In it you have the CALL QP2SHELL command.  When you use the script with a direct invocation of redis-server, the job with your CL running in it hangs waiting for the CALL to complete (which it won't because redis-server is a server that is supposed to start and stay running).

And when you try to use source or exec, the redis-server doesn't appear to start properly.

Why not just submit the CALL QP2SHELL command to batch instead of calling it from a CL?  Does that do what you want?
Sorry I have been out of the office this week.

When I just run the CALL QP2SHELL command:

CALL PGM(QP2SHELL) PARM('/redislog/startredis')

I get the following message from within the QSH screen:

/redislog/startredis[3]: /QOpensys/opt/freeware/sbin/redis-server --logfile /
redislog/redis-server.log --maxmemory 1GB:  not found.                      
Press ENTER to end terminal session.                                        

Then I have to press enter to return to the command line. The Redis server does not start...

I appreciate all your help thus far...
I apologize for the previous post. I still had the EXEC command inside of my startredis shell script.

I can not call the:

CALL       PGM(QP2SHELL) PARM('/redislog/startredis')

from a command line, but it essentially freezes the screen (with a X in the window) and stays active. It shows the following jobs running:

REDIS_TEST   QSYSOPR     BCH      .0  PGM-sh           THDW
REDIS_TEST   QSYSOPR     BCI      .0  PGM-redis-serv   SELW

The top job should not be running, but the bottom job should.  I attempted to start this via a SBMJOB command, but I get the same thing (minus the hung session).

Any ideas?
That looks right to me.  redis-server is a server-style job / a daemon / a never-ending program, and you're having trouble getting it to run in the initial shell - so let it spawn its own thread and run.  Normally you'd submit this to batch or set it up as an auto-start job.  

Is the only problem that you just don't like the look of the BCH job hanging there?  If so, just start it form batch.  you generally start a server job like this from a job scheduler or from an autostart job anyway.  The "hanging" initial shell is functioning perfectly - it is just waiting for redis-server to end, and it will terminate when it does.
When you say "Submit to batch" - do you mean from a iSeries command line?

When I do this - it freezes my screen that I submit from.

From the command line, I call the following:

CALL       PGM(QP2SHELL) PARM('/redislog/startredis')

This freezes my screen...
You use the SBMJOB command to submit a batch job.  Specific parameters to run in your environment are going to vary.  Do you have a sysadmin who can help you?
I did try submitting the job via the SBMJOB command:

SBMJOB CMD(CALL PGM(QP2SHELL) PARM('/redislog/startredistest')) JOB(REDIS_TEST) JOBQ(*LIBL/QREDIS) USER(QSYSOPR)  

When I do it this way - 2 jobs start - like I said above:

REDIS_TEST   QSYSOPR     BCH      .0  PGM-sh           THDW
REDIS_TEST   QSYSOPR     BCI      .0  PGM-redis-serv   SELW

Control is returned to the screen this way - I would just rather not have the BCH job running...
ASKER CERTIFIED SOLUTION
Avatar of Gary Patterson, CISSP
Gary Patterson, CISSP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all your help
Very helpful