How to judge whether its STDERR is redirected to a file in a Bash script on Linux?

Within a Bash script, how to judge whether its STDERR is redirected to a file in Bash on Linux? For example, ./ /tmp/log 2>&1 Can detect that its STDERR is redirected? Knowing the destination file is better. To test whether a script’s STDERR (or STDOUT) is redirected to a file, check by [[ -f /dev/stderr ]] A process’ STDOUT and STDERR are /dev/stdout and /dev/stderr and testing them with -f in Bash can judge whether they are redirected to files. » Read more

How to make CentOS Linux to load a module automatically at boot time?

How to make CentOS Linux to load a module, say ixgbe, automatically at boot time? I am using CentOS 7. You can create a text file <some name>.conf in the /etc/modules-load.d/ and list the modules to be loaded there, one per line. The systemd-modules-load.service daemon will read these files and load the modules. Check more details of modules-load.d by checking the modules-load.d man page. » Read more

How to get the highest temperature from all sensors in a server on Linux?

It is useful to monitor a server node’s temporary. Among all the sensors’ temperatures, the higher one may be a very important one. How to get the highest temperature from all sensors in a server on Linux? You can use this command to get the the highest temperature from all sensors in a server on Linux: sensors | grep '°C' | cut -d '°' -f1 | rev | cut -d' ' -f1 | rev | tr -d '+' | sort -n | tail -n1 The first line gets all the lines with a degree. » Read more

What is the vruntime of one process after it is moved into another run queue in Linux Kernel

When we do migration, one process will be migrated from one source CPU’s runqueue to destination CPU’s run queue. What is the virtual run time (if CFS is used) after it is moved into destination CPU’s run queue? When the process is dequeued from source CPU’s run queue, its vruntime will minus the minimum vruntime of source CPU’s run queue. When the process is enqueued destination CPU’s run queue, its vruntime will plus the minimum vruntime of destination CPU’s run queue. » Read more

How to output function stack in Linux Kernel

In Linux Kernel, we usually trace/debug what kind of events will trigger the phenomena we find in the system. For example, what kind of event will trigger the fact that the timeslice of one process will be very short. In order to solve these kind of problems, we need to output the function stack. Currently, there are two easy ways to solve this question. » Read more

Maximum number of mmap()’ed ranges and how to set it on Linux?

What’s the maximum number of mmap()‘ed ranges that a process can makes and how to set the limits on Linux? I have a program that mmap()s and mprotect()s lots ranges. After allocating many ranges, mprotect() starts to fail with ENOMEM error number. From the man page, ENOMEM means 2 possible problems: ENOMEM Internal kernel structures could not be allocated. » Read more

How to Add “nomodeset” to Grub2 in Fedora and Ubuntu

I need to add nomodeset to make my GPU card work on Linux. Latest Fedora and Ubuntu and their derives use grub2. How to add nomodeset permanently to grub2 on Fedora and Ubuntu? The parameters grub2 will use are stored in its grub.cfg file. The grub.cfg file is generated from grub’s default settings file /etc/default/grub. To update the kernel parameter in grub2, edit the /etc/default/grub file first. » Read more

How to make Linux automatically reboot after a kernel panic?

After a kernel panic, it is impossible to remotely connect to the Linux server to reboot it by SSH. How to make the panic kernel automatically reboot itself? Linux kernel has a nice feature that reboots itself after a timeout when a kernel panic happened. Usually, it is disabled by default. To turn it on, you can set the kernel.panic kernel parameter. » Read more

How to enlarge Linux UDP buffer size?

One of the most common causes of UDP datagram lost on Linux is an undersized receive buffer on the Linux socket. How to enlarge Linux UDP buffer size? On Linux, you can change the UDP buffer size (e.g. to 26214400) by (as root): sysctl -w net.core.rmem_max=26214400 The default buffer size on Linux is 131071. You can also make it permanent by adding this line to /etc/sysctl.conf: net.core.rmem_max=26214400 Reference: Improving UDP Performance by Configuring OS UDP Buffer Limits. » Read more

Linux timer sources

Linux supports different timer sources and a machine can have multiple ones. How to find the available Linux timer source and the current one used? Find the current timer source: $ cat /sys/devices/system/clocksource/clocksource0/current_clocksource Find all available timer sources: $ cat /sys/devices/system/clocksource/clocksource0/available_clocksource Answered by anonymous. » Read more

Force Linux to reboot

How to force Linux to reboot when the reboot command does not work. Enable the use of the magic SysRq option: # echo 1 > /proc/sys/kernel/sysrq Reboot the machine: # echo b > /proc/sysrq-trigger Answered by anonymous. Even if you could not log on the system but sshd is working, you can force the Linux to reboot by: ssh root@server_home 'echo 1 > /proc/sys/kernel/sysrq; echo b > /proc/sysrq-trigger' You can find a list of all the SysRq commands at: For example, shut off a system by ‘o’, invoke OOM killer by ‘f’. » Read more

Xen: Passing kernel parameters to DomUs

How to pass kernel parameters to Xen DomUs ? Kernel paremeter may be needed to be passed to a Xen DomU. For example, when the DomU fails to boot and we want to check it in ‘single’ mode. We can simply start the DomU with the “extra=” syntax. For example, to add ‘single’ to the kernel parameters of DomU: # xm create domu-vm.cfg extra="single" To make it permanent for the VM, we can add extra="single" to the VM’s configuration file domu-vm.cfg. » Read more

How to Compile and Install Linux Kernel from the Source Code in an Existing Linux OS

Building Linux kernel may sound a complex and geek-only thing. However, as Linux kernel itself has much less depended tools/packages compared to other software packages, it is quite easy to compile, build and install a Linux kernel from the source code in an existing Linux OS. Building Linux kernel is needed if you need to build a specific Linux kernel or update your drivers for a kernel by yourself. » Read more

How sched_setaffinity works inside of Linux Kernel

Abstract Sometimes, we may want to migrate one process/thread to one specific CPU for some specific purpose. In the Unix/Linux systems, you may choose sched_setaffinity to finish this job. This article will help you to understand how sched_setaffinity (or other APIs like pthread_setaffinity_np in user-space) works internal Linux kernel. Details -- sched_setaffinity(pid_t pid, const struct cpumask *in_mask) --- __set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask, bool check) ---- stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg) ----- migration_cpu_stop(void *data) ------ __migrate_task(struct rq *rq, struct task_struct *p, int dest_cpu) ------- move_queued_task(struct rq *rq, struct task_struct *p, int new_cpu) -------- enqueue_task(struct rq *rq, struct task_struct *p, int flags) --------- returns the new run queue of destination CPU Above character steps give a workflow of how sched_setaffinity works (how it migrates one process/thread from the run queue of source CPU to the run queue of destination CPU). » Read more

Disabling IPv6 on Fedora 17 Linux

IPv6 is enabled by default on most Linux distros. However, IPv6 is not used most of time (at least in my case). This post introduces how to disable IPv6 support on Linux (newer Kernel versions, such as in Fedora 17). IPv6 on Linux can be easily configured via sysctrl. Add these lines to /etc/sysctl.conf: net.ipv6.conf.all.disable_ipv6=1 net.ipv6.conf.default.disable_ipv6=1 Note that you will need to restart Linux to make it take effect. » Read more

Linux Flushing File System Caches

We may drop the file system caches on Linux to free up memory for applications. Kernels 2.6.16 and newer provide a mechanism via the /proc/ to make the kernel drop the page cache and/or inode and dentry caches on command. We can use this mechanism to free up the memory. However, this is a non-destructive operation that only free things that are completely unused and dirty objects will not be freed until written out to disk. » Read more