Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6864313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:53:45+00:00 2026-05-27T02:53:45+00:00

I need to run bin/psql on the command line (or script) and print its

  • 0

I need to run bin/psql on the command line (or script) and print its pg_backend_pid out, so that the pg_backend_pid can be passed to another process (run by root) as command line argument. The problem for me is that the other process needs to run after it obtains the pid. The psql (with same pid session) then runs a query after the other process has started.

The trick is that Psql needs to wait until the other process gets the pg_backend_pid and it has to remain the same session.

Can this be done through shell script or perl?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:53:45+00:00Added an answer on May 27, 2026 at 2:53 am

    You’ll want to use a coprocess in bash, or in perl with some kind of two-way pipe. In Python you can use the os.popen2 command; perl also has facilities for interacting with subprocesses over pipes. However, it’s much better to use native language database drivers like DBD::Pg or psycopg2 if at all possible.

    If you must do this in the shell, see “info bash” and search for “coprocess”.

    Here’s a quick demo bash script to get you started.

    #!/bin/bash
    set -e -u
    DBNAME=whatever_my_db_is_called
    coproc psql --quiet --no-align --no-readline --tuples-only -P footer=off --no-password "$DBNAME"
    echo 'SELECT pg_backend_pid();' >&${COPROC[1]}
    read -u ${COPROC[0]} backend_pid
    echo "Backend PID is: ${backend_pid}"
    echo "SELECT random();" >&${COPROC[1]}
    read -u ${COPROC[0]} randnum
    echo "\q" >&${COPROC[1]}
    wait %1
    echo "Random number ${randnum} generated by pg backend ${backend_pid}"
    

    The arguments to psql are to ensure it doesn’t pause for input, doesn’t interpret tabs or metachars as readline commands, and doesn’t pretty-print output so it’s easier to interact with on the shell.

    Alternately, it’s possible you don’t really need psql at all, you just need to talk to the PostgreSQL server via some kind of script. If that’s the case, it’ll be MUCH easier to just use a scripting language with a PostgreSQL database interface. In Python, for example:

    #!/usr/bin/env python
    import os
    import sys
    import psycopg2
    
    def main():
            conn = psycopg2.connect("dbname=classads")
            curs = conn.cursor()
            curs.execute("SELECT pg_backend_pid();");
            pid = curs.fetchall()[0][0]
            # Do whatever you need to here,
            # like using os.system() or os.popen() or os.popen2() to talk to
            # system commands, using curs.execute() to talk to the database, etc.
            conn.close();
    
    if __name__ == '__main__':
            main()
    

    In Perl you can use DBI and DBD::Pg to achieve a similar effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to run a Linux CLI command and get its stdout output from
I need to run a specific command for a bunch of dates. To that
I have a PHP script that I need to run every minute. I have
I have a script, that I need to run after committing to a project
Something equivalent to this command line: set PATH=%PATH%;C:\Something\bin To run my application, some thing
You can run a scala script as a linux shell script: #!/bin/sh exec scala
I have this command that i need to run cd /d C:\leads\ssh & C:\Windows\System32\cmd.exe
I need to run MSBuild from the command line using the Visual Studio Command
i need run code that will create a database and populate tables. i am
I need to run an equipment audit and to do that I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.