How to get the running process’ pid in Python? In Python, you can get the pid of the current process by import os os.getpid() From the official doc: os.getpid() Return the current process id. One example os using os.getpid() to get process own ID: $ python3 Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]
Read more
Tag: Linux
How to Debug a Bash script?
Posted onHow to debug a Bash script if it has some bugs? Common techniques like printing varibles out for checking apply for bash too. For bash, I also use 2 bash-specific techniques. Use errexit option Run the script with -e option like bash -e your-script.sh or add set -o errexit to the beginning of the script.
Read more
How to get the running process’ pid in Bash?
Posted onHow to get the running process’ pid in Bash? In Bash, you can get the process ID from the variable $$. Note that in a subshell invoked by ‘(…)‘, $$ is actually the parent process’ pid. In Bash 4, you can also use $BASHPID to get the process pid.
How to find the disk where root / is on in Bash on Linux?
Posted onQuestion: how to find the disk where the Linux’s root(/) is on in Bash? The root may be on a LVM volume or on a raw disk. 2 cases: One example: # df -hT | grep /$ /dev/sda4 ext4 48G 32G 14G 71% / For another example: # df -hT | grep /$ /dev/mapper/fedora-root ext4
Read more
How to set Zimbra web service’s hostname and port?
Posted onIn Zimbra server, how to set Zimbra web service’s hostname and port? Set Zimbra Web service’s host and port (to mail.domain.com:80 as an example) for a mail domain domain.com: zmprov md domain.com zimbraPublicServiceHostname mail.domain.com zmprov md domain.com zimbraPublicServicePort 80 Reference: https://wiki.zimbra.com/wiki/When_using_a_proxy,_the_’change_password’_box_doesn’t_load
How to get the version of Zimbra?
Posted onHow to get the version of Zimbra I am using in a Zimbra server? In Zimbra, to get the version of Zimbra you are using, you can call zmcontrol -v: $ zmcontrol -v Release 7.2.6_GA_2926.F13_64_20131203115902 F13_64 FOSS edition.
How to disable DHCP in dnsmasq on Linux?
Posted onHow to disable the DHCP service in dnsmasq on Linux? That is, to leave only dnsmasq’s DNS service. The `/etc/dnsmasq.conf` file may have lines that enable DHCP service of dnsmasq. By default, the DHCP is disabled in dnsmasq (check one example dnsmasq.conf file). To disable DHCP service in dnsmasq, in `/etc/dnsmasq.conf`, remove or disable the
Read more
Getting Hostname in C Programs in Linux
Posted onIn C, how to get the hostname of the node? In C, you may use the gethostname function. #include <unistd.h> int gethostname(char *name, size_t namelen); The gethostname() function shall return the standard host name for the current machine. The namelen argument shall specify the size of the array pointed to by the name argument. The
Read more
How to get the hostname of the node in C++?
Posted onIn C++, how to get the hostname of the node? In C++, the C way works too. However, with Boost, you can use the boost::asio::ip::host_name() function to get the hostname as a std::string: namespace boost { namespace asio { namespace ip { /// Get the current host name. BOOST_ASIO_DECL std::string host_name(); … More at http://www.boost.org/doc/libs/1_63_0/boost/asio/ip/host_name.hpp
Read more
Getting Hostname in Bash in Linux in 3 Ways
Posted onGetting the hostname of the node running a program can be useful in various scenarios, such as creating logs with the hostname, identifying which node a script is running on, or configuring a distributed system with different nodes. In Bash, there are several ways to retrieve the hostname of the machine, as mentioned in the
Read more
How to get the hostname of the node in Python?
Posted onIn Python, how to get the hostname of the node? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Example: $ python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type “help”, “copyright”, “credits” or “license”
Read more
How to get the epoch timestamp in Python?
Posted onIn Python, how to get the epoch time (the number of seconds since epoch)? In Python, you can get the epoch time by calling time.time() which return a floating number: import time print time.time() If you would like to get only the number of seconds, you may convert it to an integer. One example is
Read more
How to make sort using multiple threads to run faster?
Posted onsort supports –parallel N to run N thread. However, it seems it only uses around 100% CPU as I observed although the command specified that N threads can be used. The command is as follows cat large-file | sort –parallel `nproc` where I have 16 from nproc. How to make sort use multiple threads to
Read more
How to convert svg to png in Linux?
Posted onHow to convert a svg image file to a high-quality png file in Linux? inkscape works great with vector images (better than ImageMagikk’s convert). Try this command to convert in.svg to a 1000×1000 png image: inkscape -z -e out.png -w 1000 -h 1000 in.svg
How to get hostname in Python on Linux?
Posted onIn Python, how to get hostname as the command hostname does on Linux? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Reference: https://www.systutorials.com/dtivl/20/how-to-get-the-hostname-of-the-node?show=34#a34
How to put files with spaces in names into HDFS?
Posted onI got this error when I tried to save a file with a space in its name into HDFS: $ hdfs dfs -put -f “/home/u1/testa/test a” “/u1/testa/test a” put: unexpected URISyntaxException while the HDFS seems allow spaces in its file names: https://hadoop.apache.org/docs/r2.7.3/hadoop-project-dist/hadoop-common/filesystem/model.html . How to achieve the effect of saving the files with spaces in
Read more
How to get an environment variable in Python?
Posted onIn Python, how to get an environment variable? In Python, you may use this piece of code to get an environment variable: os.environ.get(‘ENV_MIGHT_EXIST’) or this piece of code: os.getenv(‘ENV_MIGHT_EXIST’) It will return None if the environment variable is not present. Reference and for more ways, please check https://www.systutorials.com/dtivl/13/how-to-get-an-environment-variable?show=80#answer-80 .
How to get an environment variable in Go?
Posted onIn Go lang, how to get an environment variable? To get an environment variable (e.g. “VAR”) in GO: import “os” import “fmt” fmt.Println(“VAR:”, os.Getenv(“VAR”))
How to effectively disable a Linux user account?
Posted onNot yet to totally delete the Linux user account, but how to effectively disable a Linux user account? That is, disable a user account while keep all its files/configurations. Later, if it is needed, so that I can enable it again. To effectively disable a Linux user account so that it can’t login using its
Read more
How to set process with lowest priority?
Posted onIn system, sometimes, we need backstage threads with very very low priority since it cannot preempt any normal process asks for CPU. SCHED_IDLE class satisfies this requirement which has the priority lower than “nice=19” of CFS. Following code does this. 241 void set_idle_priority(void) { 242 struct sched_param param; 243 param.sched_priority = 0; 244 int s
Read more